Web2py' Unique'设置为false后仍然强制执行约束

时间:2016-07-13 22:59:55

标签: web2py

我对web2py很新,我无法弄清楚为什么我的代码会生成一张票,有人可以帮忙吗?

最初我设置了“独特”的如果为job_title为true,但我现在的用例需要允许重复。我改变了这个独特的'约束为false但现在重复的作业标题生成票证。

这是我得到的票的一个例子。

Ticket ID
70.214.83.98.2016-07-13.22-35-40.452ce905-fbd9-4ecb-b830-04be06fc0d7a

<class 'sqlite3.IntegrityError'> UNIQUE constraint failed: job.job_title

Version
web2py™ Version 2.14.5-stable+timestamp.2016.04.14.03.26.16
Python  Python 2.7.6: /usr/local/bin/uwsgi (prefix: /usr)

回溯

Traceback (most recent call last):
  File "/home/kramer52/web2py/gluon/restricted.py", line 227, in restricted
exec ccode in environment
  File "/home/kramer52/web2py/applications/Jobs_test/controllers/appadmin.py", line 703, in <module>
  File "/home/kramer52/web2py/gluon/globals.py", line 417, in <lambda>
self._caller = lambda f: f()
  File "/home/kramer52/web2py/applications/Jobs_test/controllers/appadmin.py", line 151, in insert
if form.accepts(request.vars, session):
  File "/home/kramer52/web2py/gluon/sqlhtml.py", line 1746, in accepts
self.vars.id = self.table.insert(**fields)
  File "/home/kramer52/web2py/gluon/packages/dal/pydal/objects.py", line 726, in insert
ret = self._db._adapter.insert(self, self._listify(fields))
  File "/home/kramer52/web2py/gluon/packages/dal/pydal/adapters/base.py", line 746, in insert
raise e
IntegrityError: UNIQUE constraint failed: job.job_title

这是我的表定义:

db.define_table('job',
            Field('job_title', length=200, unique=False, requires=IS_NOT_EMPTY() ),
            Field('job_department', 'reference department'),
            Field('job_flsa_status', requires=IS_IN_SET(['Exempt', 'Non-Exempt'], zero=None) ),
            Field('job_pay_type', requires=IS_IN_SET(['Hourly', 'Salaried'], zero=None) ),
            Field('job_salary_low', 'decimal(10,2)', label='Salary (low)'),
            Field('job_salary_high', type='decimal(10,2)', label='Salary (High)'),
            Field('job_summary', type='text'),
            Field('job_years_requirement', 'integer', requires=IS_INT_IN_RANGE(0, 50), default=5, label='Years Required'),
            Field('job_education_requirement', requires=IS_IN_SET(['Some HS', 'HS Diploma', 'Some College','Associates Degree','College Degree B.A.', 'College Degree B.S.', 'Masters Degree'], zero=None), label='Education' ),
            Field('job_education_requirement_required', 'boolean', default=False, label='Required?'),
            auth.signature)

以前我为job_title设置了unique = true,格式=&#39;%(job_title)s&#39;对于工作表,我已经删除了&#39;格式&#39;

1 个答案:

答案 0 :(得分:0)

并非所有数据库都支持删除约束(例如,SQLite),因此如果您在DAL最初创建表时设置unique=True,则稍后设置unique=False将不起作用。相反,您将不得不使用其他方法来删除约束(或者在SQLite的情况下,导出数据,删除并重新创建整个表,然后导入已保存的数据)。