在 Django 中,为什么迁移到 Oracle 会导致尝试保存 ID 为空的实例?
我正在将 Django 项目从 SQLite 迁移到 Oracle,并且在 disn_requisition.save() 行上抛出一个错误,声称它的 ID 为空。我没有尝试在任何模型上手动设置或摆弄 id 字段,尽管我确实阅读了它们。
关于我需要做什么来解决这个问题有什么见解吗?
IntegrityError at /upload/storage
ORA-01400: cannot insert NULL into ("INVDB"."INVDB_DISK_REQUISITION"."ID")
Request Method: POST
Request URL: [url here]
Django Version: 1.2.3
Exception Type: IntegrityError
Exception Value:
ORA-01400: cannot insert NULL into ("INVDB"."INVDB_DISK_REQUISITION"."ID")
Exception Location: /tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/django/db/backends/oracle/base.py in execute, line 507
Python Executable: /tools/python/2.7/Linux_x86_64/bin/python
Python Version: 2.7.0
Python Path: ['/home/jhayward/invdb', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/django_filter-0.5.3-py2.7.egg', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/ez_setup-0.9-py2.7.egg', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/distribute-0.6.14-py2.7.egg', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/hgsvn-0.1.8-py2.7.egg', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/paramiko-1.7.6-py2.7.egg', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/pycrypto-2.3-py2.7-linux-x86_64.egg', '/tools/python/cx_Oracle/10g/2.6/Linux_x86_64/lib/python2.6/site-packages', '/home/jhayward', '/home/jhayward/invdb/HOME_DIRECTORY', '/home/jhayward/invdb/HOME_DIRECTORY/invdb', '/tools/python/2.7/Linux_x86_64/lib/python27.zip', '/tools/python/2.7/Linux_x86_64/lib/python2.7', '/tools/python/2.7/Linux_x86_64/lib/python2.7/plat-linux2', '/tools/python/2.7/Linux_x86_64/lib/python2.7/lib-tk', '/tools/python/2.7/Linux_x86_64/lib/python2.7/lib-old', '/tools/python/2.7/Linux_x86_64/lib/python2.7/lib-dynload', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
Server time: Thu, 30 Dec 2010 10:04:24 -0600
I'm migrating a Django project from SQLite to Oracle, and I'm getting an error thrown on the line disn_requisition.save() claiming that it has a null ID. I have not attempted to manually set or fiddle with id fields on any model, although I do read them.
Any insight on what I need to do to address this?
IntegrityError at /upload/storage
ORA-01400: cannot insert NULL into ("INVDB"."INVDB_DISK_REQUISITION"."ID")
Request Method: POST
Request URL: [url here]
Django Version: 1.2.3
Exception Type: IntegrityError
Exception Value:
ORA-01400: cannot insert NULL into ("INVDB"."INVDB_DISK_REQUISITION"."ID")
Exception Location: /tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/django/db/backends/oracle/base.py in execute, line 507
Python Executable: /tools/python/2.7/Linux_x86_64/bin/python
Python Version: 2.7.0
Python Path: ['/home/jhayward/invdb', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/django_filter-0.5.3-py2.7.egg', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/ez_setup-0.9-py2.7.egg', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/distribute-0.6.14-py2.7.egg', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/hgsvn-0.1.8-py2.7.egg', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/paramiko-1.7.6-py2.7.egg', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/pycrypto-2.3-py2.7-linux-x86_64.egg', '/tools/python/cx_Oracle/10g/2.6/Linux_x86_64/lib/python2.6/site-packages', '/home/jhayward', '/home/jhayward/invdb/HOME_DIRECTORY', '/home/jhayward/invdb/HOME_DIRECTORY/invdb', '/tools/python/2.7/Linux_x86_64/lib/python27.zip', '/tools/python/2.7/Linux_x86_64/lib/python2.7', '/tools/python/2.7/Linux_x86_64/lib/python2.7/plat-linux2', '/tools/python/2.7/Linux_x86_64/lib/python2.7/lib-tk', '/tools/python/2.7/Linux_x86_64/lib/python2.7/lib-old', '/tools/python/2.7/Linux_x86_64/lib/python2.7/lib-dynload', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages', '/tools/python/2.7/Linux_x86_64/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
Server time: Thu, 30 Dec 2010 10:04:24 -0600
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将此问题发布到[电子邮件受保护]并得到以下答案:
“问题在于 SQLite 和 Oracle 的差异,与 MySQL、PostgresQL 和 SQLite 不同,Oracle 没有自动生成的主键,为了弥补这一点,Oracle 上的 Django 对每个可识别的 Django 管理的表使用一个触发器。插入时使用 NULL 主键,并使用该表的专用序列生成键。通常,如果您从头开始创建表,Django 管理工具(syncdb、sqlall 等)将为您创建序列和触发器。如果不了解更多关于如何填充架构的信息,很难说出了什么问题,但是如果您运行“python manage.py sqlall invdb”,它应该打印出 DDL 以创建触发器和序列,并且您可以手动添加它们。 ”。
就我而言——错误可能是与此相关的二次损坏——“python manage.pysyncdb”第一次崩溃了,作为标准解决方法,我第二次运行它,当时它运行时没有报告错误。我把这个交给了另一个人,所以我不确定它是如何解决的,但我相信丢失的触发器可能是与崩溃相关的二次损坏,我想这是通过让数据库类型放置代码来解决的,所以有问题的模型有触发器。
I posted this question to [email protected] and got the following answer:
"The problem is in the difference in SQLite and Oracle. Unlike MySQL, PostgresQL, and SQLIte, Oracle doesn't have auto-generated primary keys. To make up for that, Django on Oracle uses a trigger for each Django-managed table that recognizes a NULL primary key on insert, and generates a key by using a dedicated sequence for that table. Usually if you're creating the tables from scratch, the Django management tools (syncdb, sqlall, etc) will create the sequence and trigger for you. Without knowing more about how your populated your schema, it's hard to say what went wrong, but if you run 'python manage.py sqlall invdb' it should print out the DDL to create the trigger and sequence, and you can manually add them."
In my case--and the error may be secondary damage related to this--"python manage.py syncdb" crashed the first time through, and as the standard workaround I ran it a second time, when it ran without reported error. I handed this off to another person so I'm not sure how it got resolved, but I believe that the missing trigger may be secondary damage related to the crash, and I imagine this was resolved by having a database type put code in place so the offending model had the trigger.