0

我试图在模型文件夹中分离这两个方面。我得到了:pony.orm.core.ERDiagramError:无法定义实体'Guest':数据库映射已经生成

这是代码

******* db_session.py *******

from pony import orm
from model.dbase import (Guest, Offer, Payment_method, Extra_services,
                     Voucher, Reservation, db)

db = orm.Database()

class GuestManager(object):
    @orm.db_session
    def show_guest(n):
        guest = Guest[n]
        return guest

***** dbase.py *****

from datetime import date
from pony import orm

db = orm.Database()

db.bind(provider='sqlite', filename='database.sqlite')
orm.sql_debug(True)
db.generate_mapping(create_tables=True)

class Guest(db.Entity):
    nome = orm.Required(str)
    surname = orm.Required(str)
    email = orm.Required(str, unique=True)

感谢您的帮助。

4

2 回答 2

1

这样的事情应该可以解决问题。

from pony import orm
from model.dbase import (Guest, Offer, Payment_method, Extra_services,
                         Voucher, Reservation, db)


class GuestManager(object):
    @orm.db_session
    def show_guest(n):
        guest = Guest[n]
        return guest

***** dbase.py *****

from datetime import date
from pony import orm

db = orm.Database()

db.bind(provider='sqlite', filename='database.sqlite')
orm.sql_debug(True)

class Guest(db.Entity):
    nome = orm.Required(str)
    surname = orm.Required(str)
    email = orm.Required(str, unique=True)

db.generate_mapping(create_tables=True)

你不想每次都创建你的表,除非你从一个新的数据库开始。

是一个关于回购的好问题

于 2018-01-24T12:18:56.273 回答
1

您可以在启动项目时添加数据库初始化,通常是__init__.py文件。

__init__.py

from datetime import date
from pony import orm

db = orm.Database()

def startup_database():
    db.bind(provider='sqlite', filename='database.sqlite', create_db=True)
    db.generate_mapping(create_tables=True)
    orm.sql_debug(True)

def main():
    startup_database() # If you have another methods, THIS LINE SHOULD BE THE LAST ONE

if __name__ == '__main__':
    main()

然后,在其他文件中定义您的模型:

模型.py

from pony import orm

# 'db' variable it's included by own because it's on __init__.py file
# You could migrate to another file form __init__.py and import that module here
class Guest(db.Entity):
    nome = orm.Required(str)
    surname = orm.Required(str)
    email = orm.Required(str, unique=True)

并将管理器数据库创建到另一个文件中

管理器.py

from pony import orm
from models import Guest # Add another models that you have

class GuestManager(object):
    @orm.db_session
    def show_guest(n):
        guest = Guest[n]
        return guest

您可以创建任何您喜欢的架构。我希望它对你有帮助!

于 2018-08-01T18:36:42.957 回答