使用web2py DAL.define_table()正确初始化数据库表的方法是什么?

8
我正在尝试动态构建名为db.blogdb.code的表,它们具有完全相同的SQL定义。在定义它们之后,我希望用10行随机数据填充它们,并且永远不执行该初始化代码。
我的问题是,每当我在浏览器上刷新并查看db.codedb.blog的newblog appadmin界面时,初始化代码就会执行一次:https://172.25.1.1/newblog/appadmin/select/db?query=db.code.id>0
我在newblog/models/newblog.py中初始化了db.blogdb.code
from gluon import *
from gluon.contrib.populate import populate

## initialize db.blog and db.code: 
##     At runtime, build TAGGED_TABLES (once)
TAGGED_TABLES = set(['blog', 'code'])
for tt in TAGGED_TABLES:
    if not db.get(tt, False):
        db.define_table(tt,
            Field('name', length=32, notnull=True),
            Field('value', length=65535, notnull=True),
            Field('tags', type='list:reference tag', unique=False, notnull=False),
            )

        populate(db.get(tt), 10)

        ## cross-reference db.tagged_tables to this one
        db.tagged_tables.insert(name=tt,
            database_pointer='reference %s' % tt)
        db.commit()

某种情况下,if not db.get(tt, False): 允许下面的例程被多次执行。我不明白为什么...如果表已经被创建了,那么 not db.get(tt, False) 应该是 False。 然而,web2py 从不跳过初始化代码,这意味着每次重新加载时 db.blogdb.code 都会增加 10 个条目。

问题: 为什么 if not db.get(tt, False): 没有阻止多次执行?

我在 Debian 6.0 / sqlite 3.7.3 / Cherokee 1.2.101 / uWSGI 0.9.9.3 上运行 web2py 1.99.4。

解决方案

根据 Interrobang 的答案,编写正确的方式如下:

from gluon import *
from gluon.contrib.populate import populate


TAGGED_TABLES = set(['blog', 'code'])
for tt in TAGGED_TABLES:
    # db.define_table() must be called on **every page**
    #    this sets things up in memory...
    db.define_table(tt,
        Field('name', length=32, notnull=True),
        Field('value', length=65535, notnull=True),
        Field('tags', type='list:reference tag', unique=False, notnull=False),
        )

    ## initialize db.blog and db.code: 
    ##     At runtime, populate tables named in TAGGED_TABLES (once)
    if not (db(db.get(tt).id>0).select()):
        populate(db.get(tt), 10)
        ## cross-reference db.tagged_tables to this table (named in var tt)
        db.tagged_tables.insert(name=tt,
            database_pointer='reference %s' % tt)
        db.commit()

现在db.blogdb.code保持恒定大小。

摘要

必须为每个页面呈现调用db.define_tables();我的理解是错误的(只需要运行一次将表定义写入磁盘)。


很好的解决方案总结,点个赞。 - Interrobang
@Interrobang,谢谢。我也点了赞。你的理解对于使这个工作正确起到了关键作用。 - Mike Pennington
1个回答

5

这是很好的信息,我将来可能会采用这种方法;然而,我仍然想知道为什么我的代码出了问题。 - Mike Pennington
1
虽然我不确定 db.get() 语法,但 db.define_table 不是你只运行一次来创建表格的东西。它还定义了你的模型,并且应该在每个页面加载时运行(尽管 web2py 足够聪明,如果表格存在,它不会重新创建你的表格,但它将尝试更新模型更改的表格)。因此,db.get() 没有任何可获取的内容,因为你还没有定义模型。 - Interrobang
db.get(<tablename>, False) 是我尝试检查数据库中的表是否已初始化的方法。由于 db 的行为类似于 Python 字典,这看起来像是正确的咒语;然而,根据您的输入,我需要做其他事情。 - Mike Pennington

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接