Weird errors: ForeignKey('model') vs. ForeignKey(model) Table Names with Strings

29 views
Skip to first unread message

sneilan

unread,
Mar 25, 2009, 9:43:45 PM3/25/09
to South Users
According to http://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey
I can define a foreign key to a model with a string if I haven't
defined the model yet.

The thing is, I'm not sure South is accounting for that.

I've got the following in a sample application in models.py:
__init__.pyc models.py tests.py
from django.db import models

# Create your models here.
class Object1(models.Model):
SomeText = models.TextField()
Object2 = models.ForeignKey("Object2")

class Object2(models.Model):
AnInt = models.IntegerField()

Now, when I run the initial migrations I get the following:
$ ./manage.py startmigration testapp --initial
Creating migrations directory at '/home/sneilan/webapps/goalcall/
myproject/../myproject/testapp/migrations'...
Creating __init__.py in '/home/sneilan/webapps/goalcall/myproject/../
myproject/testapp/migrations'...
+ Added model 'testapp.Object2'
+ Added model 'testapp.Object1'
Created 0001_initial.py.

$./manage migrate testapp
Running migrations for testapp:
- Migrating forwards to 0001_initial.
> testapp: 0001_initial
Traceback (most recent call last):
File "/home/sneilan/webapps/goalcall/myproject/south/migration.py",
line 243, in run_migrations
runfunc(klass.orm)
File "/home/sneilan/webapps/goalcall/myproject/../myproject/testapp/
migrations/0001_initial.py", line 21, in forwards
('SomeText', models.TextField()),
File "/home/sneilan/webapps/goalcall/myproject/south/db/generic.py",
line 121, in create_table
for field_name, field in fields
File "/home/sneilan/webapps/goalcall/myproject/south/db/generic.py",
line 272, in column_sql
sql = field.db_type()
File "/home/sneilan/webapps/goalcall/lib/python2.5/django/db/models/
fields/related.py", line 716, in db_type
rel_field = self.rel.get_related_field()
File "/home/sneilan/webapps/goalcall/lib/python2.5/django/db/models/
fields/related.py", line 609, in get_related_field
data = self.to._meta.get_field_by_name(self.field_name)
AttributeError: 'str' object has no attribute '_meta'
! Error found during dry run of migration! Aborting.

Whereas, if I take out the strings in the foreign key columns (and
reverse my objects), it works as expected.

Is there something I'm not doing right? I'm using the latest south
trunk. (I checked it out a couple hours earlier today.)

Andrew Godwin

unread,
Mar 26, 2009, 4:10:38 AM3/26/09
to south...@googlegroups.com
Nope, that should work - it's probably a bug introduced in the new ORM.
I'll look into it.

Andrew

sneilan

unread,
Mar 26, 2009, 1:21:15 PM3/26/09
to South Users
Thank you very much!

On Mar 26, 3:10 am, Andrew Godwin <and...@aeracode.org> wrote:
> Nope, that should work - it's probably a bug introduced in the new ORM.
> I'll look into it.
>
> Andrew
>
> sneilan wrote:
> > According tohttp://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey

sneilan

unread,
Mar 26, 2009, 3:49:00 PM3/26/09
to South Users
I'm gonna see if I can figure this out also. Perhaps there's just a
different function that should be used at the point of the error in
migrations.py.

Andrew Godwin

unread,
Mar 30, 2009, 7:15:36 PM3/30/09
to south...@googlegroups.com
I've done the best fix I can for this:

http://south.aeracode.org/ticket/105#comment:2

In short, if you're hand writing migrations, be sure not to use strings.
If you use startmigration, it will now replace them with the right thing.

Andrew

Sean Neilan

unread,
Apr 11, 2009, 11:56:26 PM4/11/09
to South Users
So it will indeed account for models that refer to other models
earlier in a file? Automatically?

Andrew Godwin

unread,
Apr 12, 2009, 5:39:55 AM4/12/09
to south...@googlegroups.com
Yes - the startmigration command will now resolve those into the real
models and use orm.ModelName in their place in the migration, just as it
does for direct class assignment for models that have occurred before
the ForeignKey.

i.e. you can use:

- foo = models.ForeignKey("Bar")
- foo = models.ForeignKey(Bar)

Andrew
Reply all
Reply to author
Forward
0 new messages