我有一个MySql数据库,我的Web2Py应用程序在其中存储数据。我的应用程序可以成功插入记录。现在的问题是,在检索信息时。下面是我的DAL连接和author表定义。
db = DAL('mysql://myUser:myPassWord@localhost/myDB',
migrate_enabled=true, check_reserved=['all'],fake_migrate_all=True,
migrate=False)
db.define_table('author',
Field('author_id', 'string'),
Field('name', 'string', requires=[IS_NOT_EMPTY()]),
...
Field('lang', 'string', requires=[IS_NOT_EMPTY()]),
primarykey=['author_id'])问题是,当我尝试在我的控制器中检索author表的所有信息(在SQL中是select * from author)时:
crud.search(db.author)我得到一个错误:
<class 'AttributeError'> 'Table' object has no attribute 'id'
...
query undefined
args {}
args.get <built-in method get of dict object>
table <Table author (author_id, name, screen_n...l,
url, verified, lang)>
table.id undefined在我的表定义中,您可以看到我的主键是字段author_id,它是varchar (string python),并且我在我的表DAL定义中指定为主键,如Web2Py DAL Documentation (primaryKey)中所述。但是,当我在该表中进行选择时,我得到了上面的错误。我需要做什么?我已经搜索过了,但我没有找到成功的解决方案。索梅诺内能帮我吗?谢谢。
发布于 2018-06-03 02:44:28
web2py不完全支持键控表格(即主键不是自动递增ID字段的表格)。特别是,crud.search不支持这样的表。
如果可能的话,最好在表中包含一个id类型的字段作为主键(如果不指定这样的字段,DAL会自动包含一个名为“id”的字段)。
另外,请注意,为了支持SQLFORM.grid,Crud已弃用。
https://stackoverflow.com/questions/50657350
复制相似问题