IntegrityError _id可能不是NULL

时间:2013-01-29 15:38:35

标签: django

刚开始使用Django并开始创建用于测试目的的应用程序。但是,当我向ManyToMany指定过山车并通过管理UI添加一个Park实例时,我得到一个: IntreityError,rollercoasters_id可能不是NULL

我刚刚完成了一本关于此的书,代码或多或少与书籍一致(代码在本书中有效)。

我做错了什么?

当我使用ForeignKey而不是ManyToMany

时,应用程序可以正常工作
 from django.db import models

class Company(models.Model):
    name=models.CharField(max_length=30)
    location=models.CharField(max_length=30)
    website=models.URLField()

    def __unicode__(self):
        return self.name

class Constructor(models.Model):
    name=models.CharField(max_length=30)
    location=models.CharField(max_length=30)
    contactnumber=models.CharField(max_length=30)
    website=models.URLField()   

    def __unicode__(self):
        return self.name

class RollerCoasters(models.Model):
    name=models.CharField(max_length=30)
    dateopenned=models.DateField
    type=models.CharField(max_length=30)
    builtby=models.ForeignKey(Constructor)
    dateopenned=models.DateField()

    def __unicode__(self):
        return self.name

class Parks (models.Model):
    name=models.CharField(max_length=30)
    parent=models.ForeignKey(Company)
    location=models.CharField(max_length=1000)
    rollercoasters=models.ManyToManyField(RollerCoasters)
    dateopenned=models.DateField()

    def __unicode__(self):
        return self.name

错误:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/admin/Parks/parks/add/

Django Version: 1.4.2
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'Parks',
 'django.contrib.admin',
 'django.contrib.admindocs')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in wrapper
  366.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  89.         response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in inner
  196.             return view(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapper
  25.             return bound_func(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  91.                     response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in bound_func
  21.                 return func(self, *args2, **kwargs2)
File "C:\Python27\lib\site-packages\django\db\transaction.py" in inner
  209.                 return func(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in add_view
  955.                 self.save_model(request, new_object, form, False)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in save_model
  709.         obj.save()
File "C:\Python27\lib\site-packages\django\db\models\base.py" in save
  463.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in save_base
  551.                 result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "C:\Python27\lib\site-packages\django\db\models\manager.py" in _insert
  203.         return insert_query(self.model, objs, fields, **kwargs)
File "C:\Python27\lib\site-packages\django\db\models\query.py" in insert_query
  1593.     return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  910.             cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\util.py" in execute
  40.             return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py" in execute
  344.             return Database.Cursor.execute(self, query, params)

Exception Type: IntegrityError at /admin/Parks/parks/add/
Exception Value: Parks_parks.rollercoasters_id may not be NULL

1 个答案:

答案 0 :(得分:5)

您不能只在外键和多对多字段之间切换。如果你有一个外键,Django会创建一个数据库列,例如rollercoaster_id。如果你使用多对多,Django会创建一个中间连接表。

要从外键切换到多个字段,必须执行以下操作之一:

  • 删除表,然后运行syncdb,以便Django重新创建它们。您将丢失所有数据。
  • 使用manage.py sql命令的输出作为指南
  • 自行手动更新数据库架构
  • 使用架构迁移工具South。