我正在尝试在Flask SQLAlchemy中模拟以下场景:
有SubscriptionPacks
可供购买的清单。当特定User
购买SubscriptionPack
时,他们会启动该Subscription
的实例。
模型如下:
User
可以包含多个Subscriptions
(每次只有一个有效),每个Subscription
将引用一个SubscriptionPack
。
如何在SQLAlchemy中建模?
目前,我在User.id
模型中将SubscriptionPack.id
和db.ForeignKey
引用为Subscriptions
。我在Subscriptions
表中将db.Relationship
引用为Users
。这似乎不一致和错误,并且导致我必须手动编写大量SQL语句以返回正确的结果。
有关如何做到这一点的任何帮助吗?
答案 0 :(得分:1)
对于那些偶然发现这一点的人,我所寻找的是bidirectional SQLAlchemy Association Object pattern。
这允许多对多的中间表拥有它自己存储的细节。在上面的例子中,print("Hello! My name is Pinnochio. I'm currently very young, and my communication is extremely limited.")
while True:
reply = input()
print(dictionary.get(reply, "I don't know what you said!"))
表需要是一个关联对象(拥有它自己的类)。