On 15 Apr 2014, at 13:06, robert <[email protected]> wrote:

> Hi there,
> 
> I m using SQLAlchemy V. 8.6 together with geomalcheny 2.4a second I get a call
> 
> 
> I have a a mapped class:
> 
> # tblKey2goGdataLocation
> # ----------------------------------------------------------------------------
> #
> class tblKey2goGdataLocation(Base):
>    __tablename__ = "tblKey2goGdataLocation"
>    id =  Column(Integer, nullable=False, primary_key=True)
>    location = Column(Geometry('Point'))
>    name = Column( Text )
>    description = Column( Text )
>    location_type = Column( Integer )
> 
>    companies = relation(
>        "tblCompany",
>        secondary= tblCompanyLocation.__table__,
>        backref="locations",
>    )
> 
> I try to retrieve an maped instance  using this query:
> 
> q = session.query(tblKey2goGdataLocation.__table__)
> c = tblKey2goGdataLocation.__table__.c
> q = q.filter(c['id'] == 123)
> result = q.first()
> 
> now result is of type:
> type(result)
> <class 'sqlalchemy.util._collections.KeyedTuple'>
> 
> why?

Because you are asking SQLALchemy to return a table object from its query, 
which is a somewhat odd thing to do. I'm surprised it actually returns anything 
at all :). Since id is your primary key you can just do this:

result = session.query(tblKey2goGdataLocation).get(123)

This also won't do an extra SQL query if that object already happens to have 
been loaded during the current session. if you want to filter on other columns 
use the filter() method instead:

result = session.query(tblKey2goGdataLocation).filter(tblkey2goydatalocation.id 
== 123).first()

Wichert.

-- 
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