Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SqlAlchemy表和查询问题_Python_Sqlalchemy - Fatal编程技术网

Python SqlAlchemy表和查询问题

Python SqlAlchemy表和查询问题,python,sqlalchemy,Python,Sqlalchemy,我仍然在思考SqlAlchemy,遇到了一些问题。不确定这是因为我创建的关系不正确、查询不正确还是两者都不正确 总的想法是 从一个位置到多个用户(一个位置可以有多个用户,但用户只能有一个位置) 组和用户之间的多对多(用户可以是多个组的成员,组可以有多个成员) 对于描述和用户,与上面的#2相同 我的表格创建如下: Base=declarative_Base() 班级位置(基本): __tablename_uu='location' id=列(整数,主键=True) 名称=列(字符串) group\

我仍然在思考SqlAlchemy,遇到了一些问题。不确定这是因为我创建的关系不正确、查询不正确还是两者都不正确

总的想法是

  • 从一个位置到多个用户(一个位置可以有多个用户,但用户只能有一个位置)
  • 组和用户之间的多对多(用户可以是多个组的成员,组可以有多个成员)
  • 对于描述和用户,与上面的#2相同
  • 我的表格创建如下:

    Base=declarative_Base()
    班级位置(基本):
    __tablename_uu='location'
    id=列(整数,主键=True)
    名称=列(字符串)
    group\u user\u association\u table=表格('group\u user\u association\u table',Base.metadata,
    列('group_id',Integer,ForeignKey('group.id'),
    列('user\u id',Integer,ForeignKey('user.id'))
    类别组(基本):
    __tablename_uu='组'
    id=列(整数,主键=True)
    名称=列(字符串)
    用户=关系('User',secondary=group\u User\u association\u table,backref='group')
    desc_user_association_table=表('desc_user_association',Base.metadata,
    列('desc_id',整数,ForeignKey('desc.id'),
    列('user\u id',Integer,ForeignKey('user.id'))
    等级描述(基本):
    __tablename_uuu='desc'
    id=列(整数,主键=True)
    名称=列(字符串)
    用户=关系('User',secondary=desc\u User\u association\u table,backref='desc')
    类用户(基本):
    __tablename_uu='user'
    id=列(整数,主键=True)
    用户名=列(字符串)
    location\u id=列(整数,ForeignKey('location.id'))
    groups=列(字符串,ForeignKey('group.id'))
    descs=列(字符串,ForeignKey('desc.id'))
    位置=关系('location',backref='user')
    
    以下是我如何创建数据的一些示例(所有数据都是从web上刮取的):

    location=location(id=city[1],name=city[0])#city=('name',id)
    profile=User()
    profile.id=int(str(span2class[0].a['href'][7:]))
    profile.user_name=str(span2class[0].a.img['alt']
    profile.location\u id=location.id
    g=Group(id=gid,name=str(Group.contents[0])#将组添加到组表中
    self.db_session.add(g)
    #现在,将gid添加到一个列表中,该列表将添加到最终添加到用户表的概要文件中
    profile.groups.append(str(gid))#将gid粘贴到列表中
    profile.groups=','.join(profile.groups)#将列表转换为csv字符串
    #对desc重复上述基本相同的操作
    self.db_session.add(配置文件)
    self.db_session.commit()
    
    就查询而言,我有一些基本的查询,例如:

    例如在db_session.query(User).all()中:
    打印instance.id、instance.user\u名称
    
    但在执行连接以获取(例如)特定用户的group.id和group.name.id时。。。我试过的都没用。我猜表格应该是这样的:

    db_session.query(用户、组).join('users').filter(User.id==42)
    

    但是这不起作用。

    连接从左到右起作用,因此您应该连接从
    用户到
    组的关系:

    db_session.query(User, Group).join(User.group).filter(User.id == 42)
    
    但是这会返回一个元组列表
    (,)
    ,因此如果用户属于2个或更多组,您将收到2个或更多行

    如果确实希望在一个(
    SQL
    )查询中同时加载用户及其组,更好的方法是加载用户,但将查询配置为预加载组:

    u = (session.query(User)
         .options(joinedload(User.group))
         .get(42)
         )
    
    print("User = {}".format(u))
    for g in u.group:
        print("  Group = {}".format(g))
    

    为什么
    用户
    表上有
    描述
    列?@van不太确定。可能是我试着让这段关系正常运作之前留下的。或者我在我引用的一个例子中看到了它,试图让它工作。