Flask关系模型如果不存在则多次获取或创建

发布于 2025-01-08 20:12:33 字数 568 浏览 0 评论 0原文

我刚刚开始使用 Flask 和 Flask 中的 SQLAlchemy。

因此,我使用此处的示例建立了多对多关系 http://docs .sqlalchemy.org/en/latest/orm/tutorial.html 如果您向下滚动到有关关键字和标签的部分,这就是我正在研究的内容。 到目前为止,我可以插入与我的帖子相关的新关键字,并且我正在使用附加。我知道这是错误的。所以发生的情况是,下次博客文章中出现非唯一关键字时,它将抛出有关关键字冲突的错误(因为关键字应该是唯一的)

我知道正确的方法是其他东西,我只是不知道什么。我见过一个例子 get_or_create(keyword) 基本上按关键字过滤,如果找不到则添加它。然而,我相信随着数据大小的增长,这也将是错误的。 (每次保存时都会调用一次插入)。我喜欢 SQLAlchemy 自动执行多次插入的方式。我希望保留它,但避免出现重复的密钥问题。

编辑:找到解决方案,SQLAlchemy 文档引导您出错,但解释就在那里。我已经添加了答案。

I just started with Flask and SQLAlchemy in flask.

So I have a many-to-many relationship using the example here http://docs.sqlalchemy.org/en/latest/orm/tutorial.html
If you scrolldown to the part about Keywords and tags this is what I am working on.
So far I am able to insert new Keywords related to my Post and I am using append. Which is wrong I know. So what happens is that the next time a non unique keyword occurs in a blog post it will throw an error about Conflict with Keyword (since keywords are supposed to be unique)

I know the right way is something else, I just don't know what. I have seen an example of
get_or_create(keyword) which basically filters by keyword and then adds it if not found. However I believe as data size grows this will also be wrong. (Several calls on every save with single insert). I love the way SQLAlchemy is doing multiple insert automatically. I wish to keep that but avoid this duplicate key issue.

Edit: found the solution, SQLAlchemy docs guide you towards error but the explanation is in there. I have added the answer.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落叶缤纷 2025-01-15 20:12:33

好吧,经过几个小时的反复试验,我找到了解决方案,再加上我做错了一些事情。

这就是 SQL 炼金术的工作原理。答案是合并。

制作一个标签列表作为标签模型,只要你的主键是名称或唯一的东西,它们是否存在并不重要。

tags = [Tag('a1'),Tag('a2')]

假设数据库中已经有标签 a1 但我们并不关心。我们想要的只是在相关数据不存在时插入。这就是 SQLalchemy 所做的事情。
现在您可以使用我们制作的所有标签的列表来发布帖子。如果它只是一个,那么它也是一个列表。

因此

new_post = Post('a great new post',post_tags=tags)
db.session.merge(new_post)
db.session.commit()

我使用了 Flask 语法,但想法是相同的。只需确保您没有在会话之外创建模型即可。更有可能的是,你不会这样做。

这实际上很简单,但 SQLAlchemy 文档中没有提到此示例。他们使用append(),这是错误的。这只是为了创建新标签,并且知道您不会重复。

希望有帮助。

Ok after hours of trial and error I found the solution, plus somethings I was doing wrong.

This is how SQL alchemy works. the answer is merge.

make a LIST of tags as Tag models, don;t matter if they exist as long as your primary key is name or something unique.

tags = [Tag('a1'),Tag('a2')]

Say you have Tag a1 already in DB but we don't really care. All we want is to insert if related data does not exist. WHich is what SQLalchemy does.
Now you make a Post with the LIST of ALL the tags we made. If its one only , it also is a list.

therefore

new_post = Post('a great new post',post_tags=tags)
db.session.merge(new_post)
db.session.commit()

I have used Flask syntax but the idea is same. Just make sure you are not creating the Model OUTSIDE the session. More likely, you wont do it.

This was actually simple but nowhere in the SQLAlchemy docs this example is mentioned. They use append() which is wrong. It's only to create new Tags knowing you are not making duplicates.

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文