新数据库在web2py appadmin中不显示

2 投票
1 回答
811 浏览
提问于 2025-04-17 09:04

我正在尝试设计一个新的博客数据库,并想在web2py的管理界面上进行一些测试。

  • 首先,我在web2py的管理界面创建了一个新的应用,叫做newblog
  • 接着,我创建了一个文件newblog/models/appdb.py,如下所示。
  • 然后,我访问了管理界面,地址是https://172.25.1.1/newblog/appadmin/index,以确保数据库已经创建。
  • 我查看了文件系统,发现databases/newblog.db的创建时间是新的。
  • 我在appadmin菜单中点击,查看我的新数据库:选择“web2py” > “这个应用” > “数据库”。

问题:我在newblog的数据库管理界面中没有看到我的数据库。我见过其他空的web2py数据库在appadmin界面中显示,所以我不明白为什么我的数据库没有出现。

疑问:这是正常现象吗?如果是的话,我需要采取哪些最基本的步骤才能让我的web2py数据库在appadmin中显示出来?

"""
newblog/models/appdb.py
"""
def build_new_table():
    return dict({'ugly_dict': 42})

db = DAL('sqlite://newblog.db')

## Build a table of tables, by the type of table (i.e. post, code, etc)
db.define_table('db_type',
    Field('name', length=32, notnull=True, unique=True,
        comment="Name of the database table"),

    #IS_IN_DB(db, 'db.%s.name' % db.db_type.name)),
    Field('database_pointer', notnull=True, unique=True,
        compute=build_new_table(),
        comment="Reference to the database table identified by 'name'",
        ),
    )

## Define tags for the database items
db.define_table('tags',
    Field('name', length=32, notnull=True, unique=True),
    )

1 个回答

5

听起来你有一个默认的 db.py 文件,还有一个你自己写的 appdb.py 文件。需要注意的是,模型文件是按字母顺序执行的,所以 db.py 会在你的文件之后执行。db.py 给变量 db 分配了一个不同的数据库连接,因此在 appadmin 中只显示了那个数据库。你应该要么为这两组表使用同一个数据库,要么为两个数据库连接对象使用不同的变量。例如,在 appdb.py 中,你可以这样写:

blogdb = DAL('sqlite:\\newblog.db')

如果你想让所有表都使用同一个数据库,那么只需在第一个文件中定义你的 DAL 对象(在这个例子中是 appdb.py),然后在后面的模型文件中引用它(不要重新定义它)。

撰写回答