web2py中的多列约束

时间:2013-07-24 20:58:10

标签: web2py

我正在尝试使用Anthony

发布的方法
db.define_table('ts_customer',
            Field('customer_name', 'string', length=80, required=True, unique=True))

db.define_table('ts_mileage_rate',
            Field('customer_id', 'reference ts_customer', required=True, notnull=True),
            Field('mileage_rate_year', 'integer', required=True, notnull=True,
                  requires=IS_NOT_IN_DB(
                      db(db.ts_mileage_rate.customer_id == request.vars.customer_id),
                      'ts_mileage_rate.mileage_rate_year')
            ))

我收到以下错误:

输入'exceptions.AttributeError'> 'DAL'对象没有属性'ts_mileage_rate'

知道为什么吗?

1 个答案:

答案 0 :(得分:0)

db.ts_mileage_rate.customer_id表的定义中有ts_mileage_rate,因此该表尚不存在。而是在表定义之后定义requires属性:

db.ts_mileage_rate.mileage_rate_year.requires = IS_NOT_IN_DB(
    db(db.ts_mileage_rate.customer_id == request.vars.customer_id),
    'ts_mileage_rate.mileage_rate_year')