On 6/25/14, 7:11 AM, Tara Andrews wrote:
> Hello,
>
> I am trying to use SQLAlchemy to map a database that was made
> available to me, whose contents and schema cannot be altered. The
> database has a few cases where a many-to-one relationship is treated
> as a many-to-many - that is, instead of the child key being set
> directly onto the parent record, the relationship is mapped through an
> association table. So in the mapping it looks like this:
>
> vname_association = Table('VNameFactoid', Base.metadata,
>     Column('vnameKey', Integer, ForeignKey('VariantName.vnameKey')),
>     Column('factoidKey', Integer, ForeignKey('Factoid.factoidKey')))
>
> class Factoid(Base):
>     __tablename__ = 'Factoid'
>     tstamp = Column(Time)
>     engDesc = Column(Text)
>     origLDesc = Column(Text)
>     factoidKey = Column(Integer, primary_key=True)
>     vnameInfo = relationship("VariantName",
> secondary=vname_association, backref="factoidData")
>
> class VariantName(Base):
>     __tablename__ = 'VariantName'
>     tstamp = Column(Time)
>     vnameKey = Column(Integer, primary_key=True)
>     name = Column(String)
>
>
> This means that I can get at the variant name information from the
> factoid by saying
> my_variant_name = some_factoid.vnameInfo[0].name
>
> But it would be much nicer if I could dispense with that [0], since I
> know that there will never be more than one record returned in the
> list. Is there something I can do to be able to say this?
> my_variant_name = some_factoid.vnameInfo.name

yes you can apply the "uselist=False" option to the relationship.    If
the relationship loads more than one row you'll get a warning.


>
> Best,
> -tara
> -- 
> You received this message because you are subscribed to the Google
> Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to