Opened 11 years ago
Closed 11 years ago
#22713 closed Bug (duplicate)
Unable to load initial data with ForeignKey field during migration in 1.7b4
| Reported by: | Owned by: | nobody | |
|---|---|---|---|
| Component: | Migrations | Version: | |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Here is a models.py from my really simple demoapp
from django.db import models
class SimpleModel(models.Model):
pass
class ModelWithReference(models.Model):
simplemodel = models.ForeignKey(SimpleModel, related_name='modelswithreference')
And migrations
in file 0001_initial.py
# encoding: utf8
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
]
def load_initial_data(apps, schema_editor):
simplemodel = apps.get_model('demoapp', 'SimpleModel')
simplemodel.objects.create(id=1)
simplemodel.objects.create(id=2)
operations = [
migrations.CreateModel(
name='SimpleModel',
fields=[
('id', models.AutoField(serialize=False, auto_created=True, verbose_name='ID', primary_key=True)),
],
options={
},
bases=(models.Model,),
),
migrations.RunPython(load_initial_data)
]
and file 0002_modelwithreference.py
# encoding: utf8
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('demoapp', '0001_initial'),
]
def load_initial_data(apps, shema_editor):
simplemodel = apps.get_model('demoapp', 'SimpleModel').objects.get(pk=1)
modelwithreference = apps.get_model('demoapp', 'ModelWithReference')
modelwithreference.objects.create(id=1, simplemodel=simplemodel)
operations = [
migrations.CreateModel(
name='ModelWithReference',
fields=[
('id', models.AutoField(serialize=False, primary_key=True, verbose_name='ID', auto_created=True)),
('simplemodel', models.ForeignKey(to='demoapp.SimpleModel', to_field='id')),
],
options={
},
bases=(models.Model,),
),
migrations.RunPython(load_initial_data)
]
Exception is
Running migrations:
Applying demoapp.0001_initial... OK
Applying demoapp.0002_modelwithreference...Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/core/management/__init__.py", line 427, in execute_from_command_line
utility.execute()
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/core/management/__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/core/management/base.py", line 337, in execute
output = self.handle(*args, **options)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 146, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/db/migrations/executor.py", line 62, in migrate
self.apply_migration(migration, fake=fake)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/db/migrations/executor.py", line 96, in apply_migration
migration.apply(project_state, schema_editor)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/db/migrations/migration.py", line 107, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/db/migrations/operations/special.py", line 117, in database_forwards
self.code(from_state.render(), schema_editor)
File "/home/yuriy/dev/python/projects/demo/demoapp/migrations/0002_modelwithreference.py", line 16, in load_initial_data
modelwithreference.objects.create(id=1, simplemodel=simplemodel)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/db/models/query.py", line 368, in create
obj = self.model(**kwargs)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/db/models/base.py", line 444, in __init__
setattr(self, field.name, rel_obj)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/db/models/fields/related.py", line 597, in __set__
self.field.rel.to._meta.object_name,
ValueError: Cannot assign "<SimpleModel: SimpleModel object>": "ModelWithReference.simplemodel" must be a "SimpleModel" instance.
Somehow it works if I get initial data loading from 0002_modelwithreference.py to another file, for example
file 0003_loadinitialdata.py
# encoding: utf8
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('demoapp', '0002_modelwithreference'),
]
def load_initial_data(apps, shema_editor):
simplemodel = apps.get_model('demoapp', 'SimpleModel').objects.get(pk=1)
modelwithreference= apps.get_model('demoapp', 'ModelWithReference')
modelwithreference.objects.create(id=1, simplemodel=simplemodel)
operations = [
migrations.RunPython(load_initial_data),
]
but it raises exception on first run and works on second. Result is:
(rest) yuriy@yuriy-$ rm db.sqlite3
(rest) yuriy@yuriy-$ ./manage.py migrate
Operations to perform:
Synchronize unmigrated apps: sessions, admin, auth, contenttypes
Apply all migrations: demoapp
Synchronizing apps without migrations:
Creating tables...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Installing custom SQL...
Installing indexes...
Running migrations:
Applying demoapp.0001_initial... OK
Applying demoapp.0002_modelwithreference... OK
Applying demoapp.0003_loadinitialdata...Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/core/management/__init__.py", line 427, in execute_from_command_line
utility.execute()
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/core/management/__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/core/management/base.py", line 337, in execute
output = self.handle(*args, **options)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 146, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/db/migrations/executor.py", line 62, in migrate
self.apply_migration(migration, fake=fake)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/db/migrations/executor.py", line 96, in apply_migration
migration.apply(project_state, schema_editor)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/db/migrations/migration.py", line 107, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/db/migrations/operations/special.py", line 117, in database_forwards
self.code(from_state.render(), schema_editor)
File "/home/yuriy/dev/python/projects/demo/demoapp/migrations/0003_loadinitialdata.py", line 17, in load_initial_data
modelwithreference.objects.create(id=1, simplemodel=simplemodel)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/db/models/query.py", line 368, in create
obj = self.model(**kwargs)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/db/models/base.py", line 444, in __init__
setattr(self, field.name, rel_obj)
File "/home/yuriy/dev/python/environments/rest/lib/python3.4/site-packages/django/db/models/fields/related.py", line 597, in __set__
self.field.rel.to._meta.object_name,
ValueError: Cannot assign "<SimpleModel: SimpleModel object>": "ModelWithReference.simplemodel" must be a "SimpleModel" instance.
(rest) yuriy@yuriy-$ ./manage.py migrate
Operations to perform:
Synchronize unmigrated apps: contenttypes, sessions, admin, auth
Apply all migrations: demoapp
Synchronizing apps without migrations:
Creating tables...
Installing custom SQL...
Installing indexes...
Running migrations:
Applying demoapp.0003_loadinitialdata... OK
You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): no
(rest) yuriy@yuriy-$
Note, that I removed database: rm db.sqlite3
first run ./manage.py migrate - exception, second run ./manage.py migrate - Applying demoapp.0003_loadinitialdata... OK
Attachments (1)
Change History (4)
by , 11 years ago
| Attachment: | demoapp.tar.gz added |
|---|
comment:1 by , 11 years ago
Could you try to see if the patch proposed in #22659 solves your issue.
I think both are related since CreateModel operation are special cased and might reuse model state fields which could be the source of the ValueError you're encountering.
comment:2 by , 11 years ago
Yes, it solves issues, with this patch migrations work as expected.
Thanks a lot for fast answer and solution.
comment:3 by , 11 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
Alright I'll mark this ticket as duplicate of #22659 in this case.
source code of demoapp application and migrations