新表无法识别

时间:2012-02-26 06:54:40

标签: database web2py

我在我的Web2Py应用程序中添加了一个新表:

db.define_table('users',
    db.Field('name', 'string'),
    db.Field('password', 'password'),
    db.Field('reputation', 'integer', default=0),
    db.Field('joined', 'datetime', default=datetime.utcnow())
)

引用它的旧表的字段:

db.Field('user', db.users),

但是这给了我KeyError以下跟踪:

Traceback (most recent call last):
  File "E:\Programming\Python\web2py\gluon\restricted.py", line 204, in restricted
    exec ccode in environment
  File "E:/Programming/Python/web2py/applications/vote_up/models/db.py", line 85, in <module>
    db.Field('user', db.users),
  File "E:\Programming\Python\web2py\gluon\dal.py", line 5119, in __getattr__
    return self[key]
  File "E:\Programming\Python\web2py\gluon\dal.py", line 5113, in __getitem__
    return dict.__getitem__(self, str(key))
KeyError: 'users'

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

必须在以下之前定义“用户”表:

db.Field('user', db.users)

否则,db.users在上面的代码中引用它时尚不存在。另一种选择是:

db.Field('user', 'reference users')

,不需要定义“用户”表。