On 02/10/2016 01:01 PM, Dermot O'Sullivan wrote:
Hi guys,

Been trying to work this out. I have an existing database that I would
like to use sqlalchemy on. The code below is working in the sense that I
can query individual tables and joins do not fail ( where a FK reference
exists in the db ) but I can't seem to get references working.
engine = create_engine("postgresql:///local")

metadata = MetaData()
metadata.reflect(engine, only=['a', 'b'])

Base = automap_base(metadata=metadata)

class A(Base):
     __tablename__ = 'table_a'

class B(Base):
     __tablename__ = 'table_b'

     a_ref = relationship('A', backref='b_ref')

Base.prepare(engine)

Note the 'only' in the metadata is because there are some tables in the
database causing trouble with the full reflect and I don't need them for
now so ignoring them. No errors when I load just these 2.

There is a real FK relationship in the database from B to A ( and I can
see it gets reflected properly in b.__table__.foreign_keys ) but for
some reason 'a_ref' is not defined on the instance returned by queries (
so instance.a_ref would throw an AttributeError ). I know it must be
something small I'm missing but I can't seem to get it.


if you are querying as session.query(B), there has to be an a_ref on the B objects you get back. How are you querying? Can you provide a full example of table defs and use ?



Any help would be appreciated.

Cheers,
Dermot

--
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 sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to