如何正确地使用多个文件使用相同的dectarative_base()?
这是我的文件布局的外观:
.
├── 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.py
和user.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_base
对base.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论