'Remove model inheritance and keep id in newly created autofield
I have a model which inherit from a non-abstract super model:
class Person(models.Model):
pass
class User(Person):
pass
Because of a conception mistake we want to remove the Person model and copy all person data to users. The main problem concern the "id" AutoField.
The user model doesn't have any id field autocreated by django. Because of inheritance the user's primary key is "person_ptr_id".
I'm trying to make database migrations. Django migrations want to add the "id" field as AutoField but ask for a default value. I want the initial value copied from the person_ptr_id for each user record. I also want the postgres sequence synced with values.
Have you already performed such migrations ?
Solution 1:[1]
In my experience, what worked best was:
- Rename old model and create a migration for it.
- Recreate a new model with the correct hierarchy and again, create a migration for it.
- Create a data migration and populate new model with older values.
- Remove old model and migrate again.
This was, by far, the less tortuous path. Hope it helps someone with the same problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | oscarah |
