本文介绍了在定义之前引用 web2py 中的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下,我试图为员工分配一个部门,并在部门表中创建一个经理

My code is the following im trying to assign a department to employees and also create a manager in a department table

db = DAL(lazy_tables=True)
db.define_table('employee',
            Field('fullname','string',label='Name'),
            Field('email','string'),
            Field('phone','string'),
            Field('kids', 'string'),
            Field('phone', 'string'),
            #Field('date','datetime'),
            Field('dob', 'datetime', label='Date'),
            Field('department', 'reference department',
                  requires=IS_IN_DB(db, db.department.id, '%(department_name)s')),
            auth.signature,
            format='%(fullname)s'

            )
db = DAL(lazy_tables=True)
db.define_table('department',
            Field('department_name', 'string', label='Department Name'),
            # Field('department_name', 'string', label='Department Name'),
            Field('manager', 'reference employee', required='true',
                  requires=IS_IN_DB(db, db.employee.id, '%(fullname)s')),
            auth.signature,
            format='%(department_name)s'
            )

推荐答案

如果你去掉第二个 db = DAL(lazy_tables=True),你应该没问题.

If you remove the 2nd db = DAL(lazy_tables=True), you should be fine.

您遇到的问题是您通过在第一个表定义(员工)之后再次声明/实例化它基本上覆盖了第一个 db 对象.

The problem you have is that you essentially overwrote the first db object by declaring/instantiating it again after the first table definition (employee).

这为您提供了一个新的 db 对象(未定义表),您可以在其中定义一个表(部门),该表(部门)引用另一个不再存在的表(员工).

This gives you a new db object (with no tables defined) in which you define a table (department) that references another table (employee) that no longer exists.

这篇关于在定义之前引用 web2py 中的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 15:03