Re: [sqlalchemy] Association Object creation
On Nov 23, 2011, at 5:20 AM, Enrico Morelli wrote: > On Wed, 23 Nov 2011 10:59:03 +0100 > Enrico Morelli wrote: > > >> These are the mappers: >> mapper(Ligand, ligand_table, >> properties={ >> 'ligand':relationship(LigandLigand, >> primaryjoin=and_(ligand_table.c.id==ligand_ligand_table.c.ligand_id1, >> ligand_table.c.id==ligand_ligand_table.c.ligand_id2), >> backref=backref('ligandligand') ), }) >> >> mapper(LigandLigand, ligand_ligand_table, >> properties={ >> 'first_sphere': relationship(Ligand, >> >> primaryjoin=and_(ligand_ligand_table.c.ligand_id2==ligand_table.c.id, >> >> ligand_ligand_table.c.ligand_id1==ligand_table.c.id), >> backref=backref('root')), >> }) >> >> When I try to access to the ligand.ligand properties, this is empty >> even if in the db there are relations. Where is the problem? >> >> Thanks >> >> > > I think to have solved the problem changing the logical operator in the > primaryjoin from and_ to or_. > > Now when I remove the relations I have the following warning: > SAWarning: Multiple rows returned with uselist=False for lazily-loaded > attribute > LigandLigand.ligandligand' value = callable_(passive=passive) it means a relationship is expecting many-to-one or one-to-one and getting more than one row back.Setting uselist=True on that backref would resolve this issue, though if you only intend for one row to come back there you might want to triple-check what SQL is being emitted. The configuration here is already outside the normal realm of a "relationship" between two tables (not a traditional foreign key to primary key) so you may have to change or forego the usage of relationship() here.If it were me I'd just use two relationships to represent "ligand1" and "ligand2". -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@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] Association Object creation
On Wed, 23 Nov 2011 10:59:03 +0100 Enrico Morelli wrote: > These are the mappers: > mapper(Ligand, ligand_table, >properties={ > 'ligand':relationship(LigandLigand, > primaryjoin=and_(ligand_table.c.id==ligand_ligand_table.c.ligand_id1, >ligand_table.c.id==ligand_ligand_table.c.ligand_id2), > backref=backref('ligandligand') ), }) > > mapper(LigandLigand, ligand_ligand_table, >properties={ > 'first_sphere': relationship(Ligand, > > primaryjoin=and_(ligand_ligand_table.c.ligand_id2==ligand_table.c.id, > > ligand_ligand_table.c.ligand_id1==ligand_table.c.id), >backref=backref('root')), >}) > > When I try to access to the ligand.ligand properties, this is empty > even if in the db there are relations. Where is the problem? > > Thanks > > I think to have solved the problem changing the logical operator in the primaryjoin from and_ to or_. Now when I remove the relations I have the following warning: SAWarning: Multiple rows returned with uselist=False for lazily-loaded attribute 'LigandLigand.ligandligand' value = callable_(passive=passive) What means? -- --- (o_ (o_//\ Coltivate Linux che tanto Windows si pianta da solo. (/)_ V_/_ +--+ | ENRICO MORELLI | email: more...@cerm.unifi.it | | * * * *| phone: +39 055 4574269 | | University of Florence| fax : +39 055 4574253 | | CERM - via Sacconi, 6 - 50019 Sesto Fiorentino (FI) - ITALY| +--+ -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@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] Association object
On Feb 9, 2011, at 4:34 AM, Enrico Morelli wrote: > Dear all, > > I've to create an association object where the many-to-many relation has > to be created with only one table: > > atominfo_table = Table('atom_info', metadata, >Column('id', types.Integer, primary_key=True), >Column('number', types.Integer, nullable=False), >Column('coord_x', types.Unicode(15), nullable=False), >Column('coord_y', types.Unicode(15), nullable=False), >Column('coord_z', types.Unicode(15), nullable=False), >Column('residue_id', types.Integer, > ForeignKey('residue_info.id'),primary_key=True), >Column('periodic_id', types.Integer,ForeignKey('periodic_glos.id'), > primary_key=True), ) > > atom_atom_table = Table('atom_atom', metadata, >Column('atom1_id', types.Integer, > ForeignKey('atom_info.id'),primary_key=True), >Column('atom2_id', types.Integer, ForeignKey('atom_info.id'), > primary_key=True), >Column('interaction_type', types.Unicode(50),nullable=False), >Column('distance', types.Unicode(10),nullable=False), ) > > Is it possible? If yes how can create the mapper? > The following attempt give me the error: > Could not determine join condition between parent/child tables on > relationship AtomInfo.atom1. Specify a 'primaryjoin' expression. If > 'secondary' is present, 'secondaryjoin' is needed as well. > > mapper(AtomInfo, atominfo_table, > properties={ > 'residue': relationship(ResidueInfo, backref='atominfo'), > 'periodic': relationship(Periodic, backref='atominfo'), > 'atom1': relationship(AtomAtom) >}) > > mapper(AtomAtom, atom_atom_table, > properties={ > 'atom2': relationship(AtomInfo) >}) Ideally you'd be using ForeignKeyConstraint to specify a composite foreign key. Otherwise it looks like theres multiple foreign keys between them, that's why it doesn't know how to join. http://www.sqlalchemy.org/docs/core/schema.html#defining-foreign-keys > > > Thanks > -- > --- > (o_ > (o_//\ Coltivate Linux che tanto Windows si pianta da solo. > (/)_ V_/_ > +--+ > | ENRICO MORELLI | email: more...@cerm.unifi.it | > | * * * *| phone: +39 055 4574269 | > | University of Florence| fax : +39 055 4574253 | > | CERM - via Sacconi, 6 - 50019 Sesto Fiorentino (FI) - ITALY| > +--+ > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To post to this group, send email to sqlalchemy@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. > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@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] Association object
On Wed, 9 Feb 2011 10:34:22 +0100 Enrico Morelli wrote: > mapper(AtomInfo, atominfo_table, >properties={ > 'residue': relationship(ResidueInfo, backref='atominfo'), > 'periodic': relationship(Periodic, backref='atominfo'), > 'atom1': relationship(AtomAtom) > }) > > mapper(AtomAtom, atom_atom_table, >properties={ >'atom2': relationship(AtomInfo) > }) > Using the following mapper, I haven't the error: mapper(AtomInfo, atominfo_table, properties={ 'residue': relationship(ResidueInfo, backref='atominfo'), 'periodic': relationship(Periodic, backref='atominfo'), 'atom1': relationship(AtomAtom, primaryjoin=atominfo_table.c.id==atom_atom_table.c.atom1_id) }) mapper(AtomAtom, atom_atom_table, properties={ 'atom2': relationship(AtomInfo, primaryjoin=atominfo_table.c.id==atom_atom_table.c.atom2_id) }) Is that the correct solution? Thanks again. -- --- (o_ (o_//\ Coltivate Linux che tanto Windows si pianta da solo. (/)_ V_/_ +--+ | ENRICO MORELLI | email: more...@cerm.unifi.it | | * * * *| phone: +39 055 4574269 | | University of Florence| fax : +39 055 4574253 | | CERM - via Sacconi, 6 - 50019 Sesto Fiorentino (FI) - ITALY| +--+ -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@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.