python - SQLAlchemy 和 Flask 的关系

标签 python flask flask-sqlalchemy

更新:由于我更接近的评论之一,我确实添加了关联表:

association = db.Table('association',
    db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True),
    db.Column('restaraunt_id', db.Integer, db.ForeignKey('restaraunt.id'), primary_key=True)
)

它工作得很好,在 shell 中我能够:

u=User(username='bob', email='bob@villa.com')
db.session.add(u)
db.session.commit()
r=Restaraunt(name='Five Guys', costRating=4)
db.session.add(r)
db.session.commit()
rp=RestarauntPreferences(user=u, restaraunt=r, rating=0)
db.session.add(rp)
db.session.commit()

然后:

rp.restaraunt.name
['Five Guys']

太棒了。但是... db.session.add(rp) 将重复的条目添加到 RestarauntPreferences 表中?我如何声明该关联有什么问题吗?关于 SQLAlchemy 和 Flask 还有什么我不明白的吗? (并且使用db browser查看sqlite数据库,运行shell命令后关联表中没有任何信息,不确定这是否正确......)

原帖:(以下型号)

我试图构建这个使用 SQLAlchemy 的小 Flask 应用程序。我尝试使用的模型和工作关系是多对多的。下面描述了它们以及我想要如何使用它们。

(本质上,用户可以对多个餐厅进行评分)

#how do I use these??

rprefs=RestarauntPreferences.query.filter_by(user=current_user) 
rprefs.restaraunt.name   #doesn't work, doesn't know what restaraunt is

#models below

class User(UserMixin, db.Model):
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password_hash = db.Column(db.String(128))
    recipient_idf= db.Column(db.String(256))
    first_name = db.Column(db.String(128))
    last_name = db.Column(db.String(128))
    most_recent_lunch_date = db.Column(db.DateTime)
    lunch_Rating = db.Column(db.Integer)
    phone_number = db.Column(db.String(128))

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

    def __repr__(self):
        return '<User {} {}>'.format(self.username, self.recipient_idf)


class Restaraunt(db.Model):
    __tablename__ = 'restaraunt'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(128))
    costRating = db.Column(db.Integer)

    def __repr__(self):
        return '<Restaraunt {} {}>'.format(self.name, self.costRating)


class RestarauntPreferences(db.Model):
    __tablename__ = 'restaraunt_preferences'
    id = db.Column(db.Integer, primary_key=True)

    restaraunt_id = db.Column(db.Integer, db.ForeignKey('restaraunt.id'))
    restaraunt = relationship("Restaraunt", foreign_keys=[restaraunt_id])

    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    user = relationship("User", foreign_keys=[user_id])

    rating = db.Column(db.Integer)


    def __repr__(self):
        return '<RestarauntPref {} {} {}>'.format(self.restaraunt_id, self.user_id, self.rating)

最佳答案

两者,Flask-SqlalchemySqlalchemy ,文档指出,多对多关系需要关联表才能发挥作用。尝试像示例中那样创建它,并将其作为辅助参数添加到您的声明中。

关于python - SQLAlchemy 和 Flask 的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53199667/

相关文章:

python - Flask多参数从一个url查询数据库的多列时如何避免多个if语句

python - 如何根据 flask 中的测试和生产环境分配设置?

python - 通过 Flask 和 BytesIO 显示来自 S3 的文件(图像)

mysql - Flask SQL-Alchemy 查询为我的数据库中存在的数据返回空值。可能是什么原因

python - 使用 SQLAlchemy 无法在 Flask 应用程序中调用 BaseQuery 对象

python - 如何使用 Django 运行这个 python 脚本?

python - 使用 getopt 的命令行选项和参数

python - 构建、psycopg2、postgresql

c# - 如何使用 ctypes 访问 C# dll 中的方法?

python - 在 SQLAlchemy 中定义表时,如何将函数(依赖于其他列的表达式)定义为列的默认值?