I have two models previously inherited from models.Model, and now I have reorganized them to inherit from the same base model. For this, Django uses multi-table inheritance, and I'm trying to create a schema and data migration for this. The database has data that needs to be transferred.
I know that Django creates OneToOneField, but I do not understand how it affects existing elements in the database.
Before inheritance
class BlogPost(models.Model): name = models.CharField() published_on = models.DateTimeField() class AudioFile(models.Model): file = models.FileField() published_on = models.DateTimeField()
After inheritance
class Published(models.Model): published_on = models.DateTimeField() class BlogPost(Published): name = models.CharField() class AudioFile(Published): file = models.FileField()
Migration
It was mainly a migration that was generated at startup:
./manage.py schemamigration app --auto .
Generated File:
class Migration(SchemaMigration): def forwards(self, orm): db.create_table('app_published', ( ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('published_on', self.gf('django.db.models.fields.DateTimeField')()), )) db.send_create_signal('app', ['Published']) db.delete_column('app_blogpost', 'published_on') db.delete_column('app_blogpost', 'id') db.add_column('app_blogpost', 'published_ptr', self.gf('django.db.models.fields.related.OneToOneField')(default=None, to=orm['app.Published'], unique=True, primary_key=True), keep_default=False) db.delete_column('app_audiofile', 'published_on') db.delete_column('app_audiofile', 'id') db.add_column('app_audiofile', 'published_ptr', self.gf('django.db.models.fields.related.OneToOneField')(default=None, to=orm['app.Published'], unique=True, primary_key=True), keep_default=False)
When I try to run it, it raises an IntegrityError:
column "published_ptr_id" contains null values