【发布时间】:2020-11-11 23:11:43
【问题描述】:
我已经连续 2 天为此头疼了,我仍然可以理解/找到一种方法来做到这一点,它看起来超级简单,但我显然忽略了一些东西(我对 DB 来说也是相当新的 :))。
我想要拥有 Owner 和 Pet 模型。 宠物将“所有者 ID”作为外键,而所有者将“宠物”作为关系,到目前为止一切都很好。 但现在我也希望所有者拥有一个写为“最喜欢的宠物”的“宠物 ID”。 在两个模型中都有外键(彼此键)开始产生一堆不同的问题(根据我尝试解决它的方式而有所不同,但要么是循环依赖,要么是一些多路径错误)
我还注意到,如果我避免在 Owner 模型中使用 'favourite_pet_id'-外键,只保留 favourite_pet-relationship,那么我不会在 DB 中的任何地方写这个(至少不可见),它仅作为“relationship”存在?
这样做的正确方法是什么? 提前致谢!
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Owner(db.Model):
id = db.Column(db.Integer, primary_key=True)
pets = db.relationship('Pet', foreign_keys='Pet.owner_id')
favourite_id = db.Column(db.Integer, db.ForeignKey('pet.id'))
favourite = db.relationship('Pet', uselist=False, foreign_keys='Owner.favourite_id')
class Pet(db.Model):
id =db.Column(db.Integer, primary_key=True)
owner_id = db.Column(db.Integer, db.ForeignKey('owner.id'))
owner = db.relationship('Owner', uselist=False, back_populates='pets', foreign_keys='Pet.owner_id')
o = Owner() # one owner
p1 = Pet() # pet 1
p2 = Pet() # pet 2
p1.owner=o # setting owner for pet1
p2.owner=o # setting owner for pet2
o.favourite=p2 # setting pet2 to be favourite
#db.session.add(o)
#db.session.add(p1)
#db.session.add(p2)
#db.session.commit()
print (p1.owner) # owner
print (p2.owner) # owner
print (p1) # pet 1
print (p2) # pet 2
print (o.pets) # owners pets
print (o.favourite) # favourite pet
【问题讨论】:
标签: database sqlite flask orm sqlalchemy