如何正确地使用多个文件使用相同的dectarative_base()?

发布于 2025-02-11 03:08:46 字数 1895 浏览 1 评论 0原文

这是我的文件布局的外观:

.
├── main.py
└── models
    ├── base.py
    ├── building.py
    └── user.py

在base.py中,我所拥有的只是所有其他类都使用的常见dectarative_base()。

base.py:

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

在其他模型文件中,我有一些简单的类来建模数据。

building.py:

from sqlalchemy import Column, Integer, String
from base import Base

class Building(Base):
    __tablename__ = "building"

    id = Column('id', Integer, primary_key=True, autoincrement=True)
    address = Column('address', String(50), unique=True)

if __name__ == "__main__":
    print("Imports successful on building.py")

user.py:

from sqlalchemy import Column, Integer, String
from base import Base

class User(Base):
    __tablename__ = "user"

    id = Column('id', Integer, primary_key=True, autoincrement=True)
    username = Column('username', String(50), unique=True)

if __name__ == "__main__":
    print("Imports successful on user.py")

我的Man.py只是试图导入数据模型类。

main.py:

from models.user import User
from models.building import Building

print("Imports successful on main.py")

我可以运行building.pyuser.py毫无问题,但是如果我运行main.py.py 我会得到以下追溯:

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    from models.user import User
  File "/[path to root director]/models/user.py", line 2, in <module>
    from base import Base
ModuleNotFoundError: No module named 'base'

我希望能够将数据模型类跨多个文件拆分,以使项目更有条理和更易于维护。但是,似乎他们都必须使用相同的dectarative_basebase.metadata.create_all(bind = Engine)之类的东西进行工作。我还想将这些文件与使用的文件分开声明。

正确的方法是什么?

Here's what my file layout looks like:

.
├── main.py
└── models
    ├── base.py
    ├── building.py
    └── user.py

In base.py, all I have is a common declarative_base() to be used by all other classes.

base.py:

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

In the other model files, I have some simple classes to model data.

building.py:

from sqlalchemy import Column, Integer, String
from base import Base

class Building(Base):
    __tablename__ = "building"

    id = Column('id', Integer, primary_key=True, autoincrement=True)
    address = Column('address', String(50), unique=True)

if __name__ == "__main__":
    print("Imports successful on building.py")

user.py:

from sqlalchemy import Column, Integer, String
from base import Base

class User(Base):
    __tablename__ = "user"

    id = Column('id', Integer, primary_key=True, autoincrement=True)
    username = Column('username', String(50), unique=True)

if __name__ == "__main__":
    print("Imports successful on user.py")

My main.py simply tries to import the data model classes.

main.py:

from models.user import User
from models.building import Building

print("Imports successful on main.py")

I can run building.py and user.py without issues, but if I run main.py I get the following traceback:

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    from models.user import User
  File "/[path to root director]/models/user.py", line 2, in <module>
    from base import Base
ModuleNotFoundError: No module named 'base'

I want to be able to split my data model classes across multiple files to make the project more organized and easier to maintain. However, it seems like they all have to use the same declarative_base for things like Base.metadata.create_all(bind=engine) to work. I'd also like to keep the files where the classes are declared separate from the files where they're used.

What's the correct way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文