基本上我想做的是,将2个表“ users”和“ company”联接起来,并向用户提供其相关的公司详细信息。
这是用户模型:
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的新手。请问有人知道如何纠正这个问题吗?
答案 0 :(得分:3)
首先,您需要在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',
}
}
这可以满足您的需求吗?