【发布时间】:2018-03-07 07:37:09
【问题描述】:
我是 web2py 的新手。我正在申请文件注册。每个文件都分配有一个编号,该编号在注册年份是唯一的。我试图有一个计算字符串字段,结合数字和年份,强制执行unique=True 约束。
我正在使用 SQLite,web2py 默认数据库。我的表定义:
db.define_table('doc_master_new',
Field('sr_no', 'string', unique=True, notnull=True,
compute=lambda r: str(r['inward_no']) + '/' + str(r['inward_date'].year)),
Field('inward_no', 'integer'),
Field('inward_date', 'date', default=request.now),
Field('doc_date', 'date'),
Field('doc_type', db.doc_type, requires=IS_IN_DB(db, db.doc_type, '%(type_code)s', orderby=db.doc_type.id)),
Field('applicant_type'), ## creditor/borrower/third-party
Field('no_defect', 'boolean', default=False),
Field('time_stamp', 'datetime', default=request.now)
)
和控制器:
def add_doc():
db.doc_master_new.sr_no.writable = False
db.doc_master_new.sr_no.readable = False
db.doc_master_new.time_stamp.writable = False
db.doc_master_new.time_stamp.readable = False
db.doc_master_new.no_defect.writable = False
db.doc_master_new.no_defect.readable = False
form = SQLFORM(db.doc_master_new,
labels = { 'inward_no':'SR No',
'inward_date':'SR Date',
'doc_date':'Document Date',
'doc_type':'Document Type',
}
)
if form.process().accepted:
session.flash = 'Document Added'
redirect(URL('index_n'))
return locals()
唯一约束没有被强制执行,并且相同的值被插入到表中。我不明白为什么。 SQLite 文档说 NULL 值被认为与所有其他值(包括其他 NULL)不同,因此添加了 notnull 约束,但仍然允许重复。
有人可以帮忙吗?
【问题讨论】: