Web2py 'Unique' 约束在设置为 false 后仍然被强制执行
Web2py 'Unique' constraint still being enforced after set to false
我是 web2py 的新手,我不明白为什么我的代码会生成票证,有人可以帮忙吗?
最初我为 job_title 将 'unique' 设置为 true 但我现在的用例要求允许重复。我将 'unique' 约束更改为 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
这是我的 table 定义:
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 并为作业 table 设置了 format='%(job_title)s',此后我删除了 'format'
并非所有数据库都支持删除约束(例如 SQLite),因此如果您在 DAL 最初创建 table 时设置 unique=True
,则稍后设置 unique=False
将无效。相反,您将不得不使用一些其他方法来删除约束(或者在 SQLite 的情况下,导出数据,删除并重新创建整个 table,然后导入保存的数据)。
我是 web2py 的新手,我不明白为什么我的代码会生成票证,有人可以帮忙吗?
最初我为 job_title 将 'unique' 设置为 true 但我现在的用例要求允许重复。我将 'unique' 约束更改为 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
这是我的 table 定义:
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 并为作业 table 设置了 format='%(job_title)s',此后我删除了 'format'
并非所有数据库都支持删除约束(例如 SQLite),因此如果您在 DAL 最初创建 table 时设置 unique=True
,则稍后设置 unique=False
将无效。相反,您将不得不使用一些其他方法来删除约束(或者在 SQLite 的情况下,导出数据,删除并重新创建整个 table,然后导入保存的数据)。