(Django)每次我和South一起迁移时,我都会遇到一个关于fixtures / initial_data.json的错误

时间:2012-03-16 05:30:25

标签: django django-south

我目前在fixtures/initial_data.json下有一个文件来实例化模型中的数据。我知道json没有问题,因为第一次运行./manage syncdb时一切顺利。

我将应用程序转换为使用South后,每次运行./manage migrate doors时都会得到此回溯:

Running migrations for doors:
 - Migrating forwards to 0002_auto__del_field_userprofile_status.
 > doors:0002_auto__del_field_userprofile_status
 - Loading initial data for doors.
Problem installing fixture '/Users/hobbes3/Sites/mysite/doors/fixtures/initial_data.json': Traceback (most recent call last):
  File "/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 174, in handle
    obj.save(using=using)
  File "/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/core/serializers/base.py", line 165, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/db/models/base.py", line 526, in save_base
    rows = manager.using(using).filter(pk=pk_val)._update(values)
  File "/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/db/models/query.py", line 491, in _update
    return query.get_compiler(self.db).execute_sql(None)
  File "/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 869, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql
    cursor.execute(sql, params)
  File "/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/db/backends/util.py", line 34, in execute
    return self.cursor.execute(sql, params)
  File "/Users/hobbes3/.virtualenvs/doors/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
    return self.cursor.execute(query, args)
IntegrityError: null value in column "created" violates not-null constraint

现在我只是忽略了错误,因为迁移仍然说它成功了:

(doors)hobbes3@hobbes3 ~/Sites/mysite $ python manage.py migrate doors --list

 doors
  (*) 0001_initial
  (*) 0002_auto__del_field_userprofile_status

我不确定South是否尝试重新加载JSON而不先截断表(并且存在主键冲突?)。但该错误听起来像是南方试图插入created字段设置为NULL的数据。我不知道为什么会这样做。

我不确定这是否有用,但这是我的initial_data.json

[
    {
        "model" : "doors.usertype",
        "pk" : 1,
        "fields" : {
          "name" : "tenant"
        }
    },
    {
        "model" : "doors.usertype",
        "pk" : 2,
        "fields" : {
          "name" : "property manager"
        }
    },
    {
        "model" : "doors.usertype",
        "pk" : 3,
        "fields" : {
          "name" : "property owner"
        }
    },
        {
        "model" : "doors.usertype",
        "pk" : 4,
        "fields" : {
          "name" : "vendor"
        }
    },
    {
        "model" : "doors.usertype",
        "pk" : 5,
        "fields" : {
          "name" : "web user"
        }
    },
    {
        "model" : "doors.doorsgroup",
        "pk" : 1,
        "fields" : {
            "name"                : "Moderator",
            "comment"             : "Moderators can create, edit, and delete all orders. They can also modify users, vendors, and places. They can comment on any orders and delete comments. They can view and manage all users.",
            "view_all_users"      : true,
            "manage_all_users"    : true,
            "view_all_orders"     : true,
            "create_orders"       : true,
            "edit_all_orders"     : true,
            "manage_orders"       : true,
            "delete_orders"       : true,
            "create_comments"     : true,
            "create_any_comments" : true,
            "delete_comments"     : true,
            "view_vendors"        : true,
            "modify_vendors"      : true,
            "view_places"         : true,
            "modify_places"       : true
        }
    },
    {
        "model" : "doors.doorsgroup",
        "pk" : 2,
        "fields" : {
            "name"            : "Manager",
            "comment"         : "Managers can view, edit, and delete all orders. They can also approve, reject, or close orders. They can view all users.",
            "view_all_users"  : true,
            "view_all_orders" : true,
            "create_orders"   : true,
            "edit_all_orders" : true,
            "manage_orders"   : true,
            "delete_orders"   : true,
            "create_comments" : true,
            "view_vendors"    : true,
            "view_places"     : true
        }
    },
    {
        "model" : "doors.doorsgroup",
        "pk" : 3,
        "fields" : {
            "name"            : "Tenant",
            "comment"         : "Tenants can create orders, comment on their orders, and view vendors and places.",
            "create_orders"   : true,
            "create_comments" : true,
            "view_vendors"    : true,
            "view_places"     : true
        }
    },
    {
        "model" : "doors.doorsgroup",
        "pk" : 4,
        "fields" : {
            "name"            : "Vendor",
            "comment"         : "Vendors can only comment on the orders that are assigned to them.",
            "create_comments" : true
        }
    },
    {
        "model" : "doors.doorsgroup",
        "pk" : 5,
        "fields" : {
            "name"            : "Observer",
            "comment"         : "Observers can view all users, orders, vendors, and places.",
            "view_all_users"  : true,
            "view_all_orders" : true,
            "view_vendors"    : true,
            "view_places"     : true
        }
    }
]

我的models.py

的一部分
class DoorsGroup( models.Model ) :
    def __unicode__( self ) :
        return self.name

    name    = models.CharField( max_length = 135 )
    comment = models.TextField( blank = True )

    # Permissions.
    view_all_users   = models.BooleanField()
    manage_all_users = models.BooleanField() # Assign to places or to vendors.

    view_all_orders = models.BooleanField()
    create_orders   = models.BooleanField()
    edit_all_orders = models.BooleanField()
    manage_orders   = models.BooleanField() # Can approve, reject, and close orders.
    delete_orders   = models.BooleanField()

    create_comments     = models.BooleanField()
    create_any_comments = models.BooleanField() # Can comment on any orders.
    delete_comments     = models.BooleanField()

    view_vendors   = models.BooleanField()
    modify_vendors = models.BooleanField() # Can create, edit, and delete vendors.

    view_places   = models.BooleanField()
    modify_places = models.BooleanField()

    created  = models.DateTimeField( auto_now_add = True )
    modified = models.DateTimeField( auto_now = True )

class UserType( models.Model ) :
    def __unicode__( self ) :
        return self.name

    name     = models.CharField( max_length = 135 )
    created  = models.DateTimeField( auto_now_add = True )
    modified = models.DateTimeField( auto_now = True )

1 个答案:

答案 0 :(得分:4)

South总是尝试在迁移后插入找到的灯具。对于您的情况,错误是由迁移后可能过时的json fixture和新的DB状态的架构不兼容引起的。可以通过

跳过灯具的插入
./manage.py migrate doors --no-initial-data

或手动修理灯具。