[sqlalchemy] begginers query question
Hi guys. I've got a little problem with the sqlalchemy syntax. I've got two tables with relations: Table_Goups id int(4) PrimaryKey not null, name varchar(50) not null; and Table_User id int(4) Primary Key not null, login varchar(50) not null, id_group int(4) Foreign Key not null; i would like to build a query that would gets the user data from Table_User and the id_group but insted od showing the id_group number i want to show the Table_Groups.name Can anyone help? -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] begginers query question
Hi, On 24/08/2010 10:14, Dobrysmak wrote: Hi guys. I've got a little problem with the sqlalchemy syntax. I've got two tables with relations: Table_Goups id int(4) PrimaryKey not null, name varchar(50) not null; and Table_User id int(4) Primary Key not null, login varchar(50) not null, id_group int(4) Foreign Key not null; i would like to build a query that would gets the user data from Table_User and the id_group but insted od showing the id_group number i want to show the Table_Groups.name Can anyone help? I use SA declarative, so you would define something like this in your model: class Group(Base): __table__ = sa.Table(u'groups', metadata, sa.Column(u'id', sa.Integer(), sa.Sequence('gen_contact_id'), primary_key=True, nullable=False), sa.Column(u'name', sa.String(length=70, convert_unicode=False), nullable=False), ) class User(Base): __table__ = sa.Table(u'users', metadata, sa.Column(u'id', sa.Integer(), sa.Sequence('gen_contact_id'), primary_key=True, nullable=False), sa.Column(u'name', sa.String(length=70, convert_unicode=False), nullable=False), sa.Column(u'fk_group', sa.Integer(), sa.ForeignKey(u'groups.id'), nullable=False), ) group = sao.relation('Group', backref='user') And then to query you could do e.g. this: for usr in session.query(db.User).all(): print 'user: %s, group name: %s' % (usr.name, usr.group.name) Check out the SA doc, especially the tutorials: http://www.sqlalchemy.org/docs/ormtutorial.html http://www.sqlalchemy.org/docs/ormtutorial.html#creating-table-class-and-mapper-all-at-once-declaratively Hope this helps Werner -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] begginers query question
On 24/08/2010 15:53, werner wrote: Hi, On 24/08/2010 10:14, Dobrysmak wrote: Hi guys. I've got a little problem with the sqlalchemy syntax. I've got two tables with relations: Table_Goups id int(4) PrimaryKey not null, name varchar(50) not null; and Table_User id int(4) Primary Key not null, login varchar(50) not null, id_group int(4) Foreign Key not null; i would like to build a query that would gets the user data from Table_User and the id_group but insted od showing the id_group number i want to show the Table_Groups.name Can anyone help? I use SA declarative, so you would define something like this in your model: class Group(Base): __table__ = sa.Table(u'groups', metadata, sa.Column(u'id', sa.Integer(), sa.Sequence('gen_contact_id'), primary_key=True, nullable=False), sa.Column(u'name', sa.String(length=70, convert_unicode=False), nullable=False), ) Oops, a copy/paste error, should be: sa.Sequence('gen_group_id') class User(Base): __table__ = sa.Table(u'users', metadata, sa.Column(u'id', sa.Integer(), sa.Sequence('gen_contact_id'), primary_key=True, nullable=False), and another one, a copy/paste error, should be: sa.Sequence('gen_user_id') sa.Column(u'name', sa.String(length=70, convert_unicode=False), nullable=False), sa.Column(u'fk_group', sa.Integer(), sa.ForeignKey(u'groups.id'), nullable=False), ) group = sao.relation('Group', backref='user') And then to query you could do e.g. this: for usr in session.query(db.User).all(): print 'user: %s, group name: %s' % (usr.name, usr.group.name) Check out the SA doc, especially the tutorials: http://www.sqlalchemy.org/docs/ormtutorial.html http://www.sqlalchemy.org/docs/ormtutorial.html#creating-table-class-and-mapper-all-at-once-declaratively Hope this helps Werner -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.