我有两个表用户和组织。
我有以下 SQL 映射类:
class User(db.Model):
__tablename__ = 'user'
id = db.Column('id', db.Integer, primary_key=True)
first_name = db.Column('first_name', db.String)
second_name = db.Column('second_name', db.String)
last_name = db.Column('last_name', db.String)
# organization_id = db.Column('organization_id', db.Integer)
organization_id = db.Column('organization_id',db.Integer,db.ForeignKey('organization.id'),nullable=False)
# organization = db.relationship('Organization',backref='user',lazy=False)
email = db.Column('email', db.String)
mobile_no = db.Column('mobile_no', db.String)
designation = db.Column('designation', db.String)
role_id = db.Column('role_id', db.Integer)
password = db.Column('password', db.String)
class Organization(db.Model):
__tablename__ = 'organization'
id = db.Column('id',db.Integer,primary_key=True)
name = db.Column('name',db.String)
type_id = db.Column('type_id',db.Integer)
ward_id = db.Column('ward_id',db.Integer)
attrs = ['id','name','type_id','ward_id']
user_organization = db.relationship('User',backref='organization',lazy=False)
我想对 User.onrganization_id 和 Organization.id 执行连接操作,结果如下所示
用户 ID | user.first_name 组织名称 | 用户.email | user.mobile_no
我试过这个代码:
q = db.session.query(User,Organization).join(Organization).all()
这给了我一个带有对象集合的列表的结果:
[(<User 2>, <Organization 1>)]
我希望返回类型是单个对象的列表,而不是对象的集合。像这样
[(<UsersAndOrganization1>)] //Name of the object doesn't have to be the same
相关分类