2015-02-11 52 views
2

我有一个用Django 1.6编写的项目,使用南迁移,我试图将它移动到Django 1.7。所以我从here指示开始。从南到Django 1.7迁移:可替换的依赖关系

  1. 删除southINSTALLED_APPS
  2. 删除旧的迁移文件。
  3. Ran ./manage.py makemigrations

在这一点上,我得到了django.db.migrations.graph.CircularDependencyError

这里是我的模型:

customer.models.py

class Customer(models.Model): 
    name = models.CharField(
     max_length=128, 
    ) 

class Department(models.Model): 
    customer = models.ForeignKey(
     'customer.Customer', 
     related_name='departments', 
    ) 
    name = models.CharField(
     max_length=64, 
    ) 

class Representative(models.Model): 
    user = models.ForeignKey(
     'userprofile.User', 
     related_name='representatives', 
    ) 
    department = models.ForeignKey(
     'customer.Department', 
     related_name='representatives', 
    ) 

userprofile.models.py

class User(AbstractBaseUser, PermissionsMixin): 
    customers = models.ManyToManyField(
     'customer.Customer', 
     blank=True, 
     null=True, 
    ) 

,在最初的迁移造成了customer应用可更换的依赖性:

dependencies = [ 
    migrations.swappable_dependency(settings.AUTH_USER_MODEL), 
] 

至于有人建议here,我编辑初始迁移userprofile和评价与客户相关的线路:

class Migration(migrations.Migration): 

    dependencies = [ 
     ('auth', '0001_initial'), 
     #('customer', '0001_initial'), 
    ] 

    operations = [ 
     migrations.CreateModel(
      name='User', 
      fields=[ 
       ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), 
       ('first_name', models.CharField(max_length=128, error_messages={b'min_length': 'El campo "Nombres" debe tener al menos %(limit_value)d caracteres (actualmente tiene %(show_value)d).'}, verbose_name='nombres', validators=[django.core.validators.MinLengthValidator(3)])), 
       ('last_name', models.CharField(max_length=128, error_messages={b'min_length': 'El campo "Apellidos" debe tener al menos %(limit_value)d caracteres (actualmente tiene %(show_value)d).'}, verbose_name='apellidos', validators=[django.core.validators.MinLengthValidator(3)])), 
       ('email', models.EmailField(unique=True, max_length=75, verbose_name='correo electr\xf3nico')), 
       #('customers', models.ManyToManyField(to='customer.Customer', null=True, verbose_name='clientes relacionados', blank=True)), 
      ], 
      bases=(models.Model,), 
     ), 
    ] 

./manage.py migrate并创造了另一个迁移,增加了客户现场:

class Migration(migrations.Migration): 

    dependencies = [ 
     ('customer', '0001_initial'), 
     ('userprofile', '0001_initial'), 
    ] 

    operations = [ 
     migrations.AddField(
      model_name='user', 
      name='customers', 
      field=models.ManyToManyField(to='customer.Customer', null=True, verbose_name='clientes relacionados', blank=True), 
      preserve_default=True, 
     ), 
    ] 

但是,当我跑./manage.py migrate userprofile --fake,我得到一个错误

Running migrations: 
    No migrations to apply. 
    Your models have changes that are not yet reflected in a migration, and so won't be applied. 
    Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them. 

在另一方面,如果没有这个迁移我的测试失败:

OperationalError: no such table: userprofile_user_customers 

回答

2

我的错误是,而不是运行./manage.py makemigrations userprofile --empty运行./manage.py makemigrations userprofile。在第一种情况下的Django理解它这样增加了contracts字段(它是)迁移和用于第二情况下,如果我跑./manage.py migrate userprofile与它失败:

django.db.utils.ProgrammingError: relation "userprofile_user_customers" already exists 

所以我不得不:

  1. 复制上次迁移的内容:

    class Migration(migrations.Migration): 
    
        dependencies = [ 
         ('customer', '0001_initial'), 
         ('userprofile', '0001_initial'), 
        ] 
    
        operations = [ 
         migrations.AddField(
          model_name='user', 
          name='customers', 
          field=models.ManyToManyField(to='customer.Customer', null=True, verbose_name='clientes relacionados', blank=True), 
          preserve_default=True, 
         ), 
        ] 
    
  2. 删除迁移。

  3. 运行./manage.py makemigrations userprofile --empty
  4. 粘贴并运行./manage.py migrate userprofile --fake