3

I'm trying to create an empty copy of my Django database using the command

manage.py syncdb --migrate --database=<db_copy_name>

It works fine if the database is a sqlite3 database, but fails for a postgres database.

With postgres the normal Django syncdb creation of tables goes fine, but the migration part fails. One of the apps has a reference to a table in another app and the other app has a reference to a table in the first app, so whichever order I migrate the apps I get a

django.db.utils.DatabaseError: relation "<table_name>" does not exist

I'm running Django 1.4, South 0.7.6, psycopg2 2.4.1, with a postgres 9.1 db. The problem is the same with South 0.8, 0.8.1, and psychopg2 2.5.

Chris Travers
  • 25,424
  • 6
  • 65
  • 182
JivanAmara
  • 1,065
  • 2
  • 10
  • 20

2 Answers2

4

Try doing a lazy import instead. Move the import statement from the top of the file into the model class that needs it

so instead of

from other_app import foo

class Table(models.Model):

    foo = models.ForeignKey(foo)

do this

class Table(models.Model):
    from other_app import foo

    foo = models.ForeignKey(foo)
ravron
  • 11,014
  • 2
  • 39
  • 66
AdonisN
  • 489
  • 4
  • 12
2

If you want to have Related Fields like ForeignField or ManyToManyField, use '<app_label>.<model_name>' string instead of importing model to reference. For example, instead of having:

from myapp.models import MyModel

class AnotherModel(Model):
    my_model = ForeignField(MyModel)

write:

class AnotherModel(Model):
    my_model = ForeignField('myapp.MyModel')

This prevents dependency of importing model in body of class. And if you want to use other model in any function, just import it inside the function body instead of module.

mhsekhavat
  • 977
  • 13
  • 18