gpt4 book ai didi

flask - Flask-SQLAlchemy-棉花糖嵌套模式不起作用

转载 作者:行者123 更新时间:2023-12-03 17:14:14 25 4
gpt4 key购买 nike

基本上我想做的是,加入2个表“用户”和“公司”,并获得用户及其相关公司详细信息。

这是用户模型:

class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
firstname = db.Column(db.String(10), nullable=False)
lastname = db.Column(db.String(25), nullable=False)
username = db.Column(db.String(250), nullable=False)
email = db.Column(db.String(100), nullable=False)
password = db.Column(db.String(250), nullable=False)
isPasswordReset = db.Column(db.Boolean, nullable=False)
companyId = db.Column(db.Integer, db.ForeignKey(
'company.id'), nullable=True)


这是架构

class UserSchema(ma.Schema):
id = fields.Integer()
firstname = fields.String(required=True)
lastname = fields.String(required=True)
username = fields.String(required=True)
email = fields.String(required=True)
password = fields.String(required=True)
isPasswordReset = fields.Boolean(required=True)
companyId = fields.Integer()
company_name = fields.Nested(CompanySchema)


这是公司模型:

class Company(db.Model):
__tablename__ = 'company'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
companyName = db.Column(db.String(100), nullable=True)
companyCode = db.Column(db.String(20), nullable=True)
companyTheme = db.Column(db.String(300), nullable=True)


这是公司架构:

class CompanySchema(ma.Schema):
id = fields.Integer()
companyName = fields.String(required=True)
companyCode = fields.String(required=True)
companyTheme = fields.String(required=True)


这是resource-user.py:

users_schema = UserSchema(many=True)
user_schema = UserSchema()

class UsersResource(Resource):
def get(self):
users = db.session.query(User.firstname, Company.companyName).join(
Company, User.companyId == Company.id).all()
if users:
results = users_schema.dump(users).data
return {'status': 'success', 'message': json.dumps(results, default=str)}, 200


这是我得到的输出:


{
“ status”:“成功”,
“消息”:“ [{\”名字\“:\” abc \“},{\”名字\“:\” xyz \“}]”
}


只显示了用户表,而未显示公司表。但仍然无法解决。我是flask和sqlalchemy的新手。请问有人知道如何纠正这个吗?

最佳答案

首先,您需要在sqlalchemy模型中声明关系对象:

class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
firstname = db.Column(db.String(10), nullable=False)
lastname = db.Column(db.String(25), nullable=False)
username = db.Column(db.String(250), nullable=False)
email = db.Column(db.String(100), nullable=False)
password = db.Column(db.String(250), nullable=False)
isPasswordReset = db.Column(db.Boolean, nullable=False)
companyId = db.Column(db.Integer, db.ForeignKey(
'company.id'), nullable=True)
company = db.relationship("Company", backref="parents") # <--


然后在用户架构中声明代表该对象的字段:

class UserSchema(ma.Schema):
id = fields.Integer()
firstname = fields.String(required=True)
lastname = fields.String(required=True)
username = fields.String(required=True)
email = fields.String(required=True)
password = fields.String(required=True)
isPasswordReset = fields.Boolean(required=True)
companyId = fields.Integer()
company = fields.Nested(CompanySchema) # <--


请注意,字段名称必须相同(或者您可以使用 attribute参数)。

查询时,您可以简单地执行以下操作:

user_schema = UserSchema()
users = User.query.all()
results = users_schema.dump(users).data
return {'status': 'success', 'message': json.dumps(results, default=str)}, 200


(或 return users_schema.dump(users)用于测试)

毕竟,您应该获得如下输出:

{
id: 'something',
firstname: 'something',
lastname: 'something',
username: 'something',
email: 'something',
password: 'something',
isPasswordReset: 'something',
company : {
id: 'something',
companyName: 'something',
companyCode: 'something',
companyTheme: 'something',
}
}


这可以满足您的需求吗?

关于flask - Flask-SQLAlchemy-棉花糖嵌套模式不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53224028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com