将导入的类添加到元数据

发布于 2025-01-03 16:00:06 字数 1171 浏览 1 评论 0原文

所有,

我从一个单独的文件导入一组 sqlalchemy 类。它们在我的数据库上定义表(继承自 declarative_base()),并且最初位于与我的引擎和元数据创建相同的文件中。

由于我有很多表,并且每个表都很复杂,所以我不希望它们位于我使用它们的同一个文件中。这使得在文件中工作更加笨拙,并且我想要更清晰的描述,因为这些类记录了当前模式。

我将它们重构为自己的文件,突然元数据无法自动找到它们。 按照此链接,我发现这是因为我的主文件声明了基数:

from tables import address, statements
Base = declarative_base()
metadata = MetaData()
Base.metadata.create_all()

我的表文件也是如此:

Base = declarative_base()
class address(Base):
    ...

所以,据我所知,他们有单独的“基础”,这就是元数据无法找到并创建声明的表的原因。我已经进行了一些谷歌搜索,看起来这应该是可能的,但没有任何明显的方法可以实现。

如何导入在单独文件中定义的表?

更新:

我尝试过这个,它有点功能。

在表文件中,为要导入的表类声明一个 Base:

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

然后在主文件中,导入预先存在的 Base 并将其元数据提供给新的 Base:

from sqlalchemy.ext.declarative import declarative_base
from tables import Base as tableBase
Base = declarative_base(metadata=tableBase.metadata)

经过更多测试,我发现这种方法遗漏了重要信息。我已经回到一个包含所有内容的文件,因为它确实可以正常工作。我将保留这个问题,以防有人能够提出正确的答案。或者,指向适当的文档。

All,

I'm importing a group of classes for sqlalchemy from a separate file. They define the tables on my DB (inheriting from declarative_base()), and were originally located in the same file as my engine and metadata creation.

Since I have quite a few tables, and each of them is complex, I don't want them located in the same file I'm using them in. It makes working in the file more unwieldy, and I want a more clear delineation, since the classes document the current schema.

I refactored them to their own file, and suddenly the metadata does not find them automatically. Following this link, I found that it was because my main file declares base:

from tables import address, statements
Base = declarative_base()
metadata = MetaData()
Base.metadata.create_all()

And so does my tables file:

Base = declarative_base()
class address(Base):
    ...

So, as far as I can tell they get separate "bases" which is why the metadata can't find and create the declared tables. I've done some googling and it looks like this should be possible, but there isn't any obvious way to go about it.

How do I import tables defined in a separate file?

Update:

I tried this, and it sort of functions.

In the tables file, declare a Base for the table classes to import:

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

Then in the main file, import the preexisting base and give its metadata to a new Base:

from sqlalchemy.ext.declarative import declarative_base
from tables import Base as tableBase
Base = declarative_base(metadata=tableBase.metadata)

After some more testing, I've found this approach leaves out important information. I've gone back to one file with everything in it, since that does work correctly. I'll leave the question open in case someone can come up with an answer that does work correctly. Or alternatively, point to the appropriate docs.

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

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

发布评论

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