hi Michael,

strangely enough when querying like you hinted i only get one row "uniqueified" again.

So i must be missing still more of how this should work. My database nicely shows two rows queried with sql (Products and Products_descripition joined, filtered by a products_id), with python sqla i only see one row. How can this be? Also, when i add a products_description object to the session it first has to be added to the Products.product_descriptions relation? I would expect adding a products_description (in which a products_id is in the relationship) would automagically update the products.product_descriptions result.

I use session.query(Products, Products_description).all(), which results in one tuple (Products, Products_description). There are two products_descriptions in the db. I wonder if i missed the most simple case in which everything is directly reflected, the db completely and always in sink with the programming in Python,

sorry for missing the point consistently appearently,
this should be really easy, but i am clueless.

Could you give me a hint?

regards
freddy



-----Original Message-----
From: Michael Bayer <[email protected]>
To: [email protected]
Sent: Sun, Feb 20, 2011 7:30 pm
Subject: Re: [sqlalchemy] difference between query.count() and query.all(), concurrency?


On Feb 20, 2011, at 8:40 AM, [email protected] wrote:

Hi listmembers,

i have a problem with a difference i find playing with sqla between
query.count and query.all results.
I have a parent class declaratively mapped to a table in MySQL, there
is a child class with a relationship.
I'm not sure how to create a quick session (i suspect a problem lies
there, but i have no clue) - my usecase is very general, but i am not sure if i have the lingo right (been reading the docs till blue in the face, didn't know using this wonderful stuff is so intricate).

In short i use the following, (productdescriptions depending on
languages):

Session = scoped_session(sessionmaker())
session = Session(autoflush=True)

class Products(Base):
 products_id = Column(Integer, primary_key=True)
 products_loc = Column(String)
 product_descriptions = relationship("Products_description",
                                      cascade="all",
                                      backref=backref('product'))

class Products_description(Base):
 products_description_id = Column(Integer, primary_key=True)
products_id = Column(Integer, ForeignKey('products.products_id'),
nullable=False)
language_id = Column(Integer, ForeignKey('languages.languages_id'),
nullable=False)

class Languages(Base):
 languages_id = Column(Integer, primary_key=True)
 etc..


I would expect the product_descriptions to hold two objects if there
are two records in de database (for two languages, but that is irrelevant here .. or is it?), related via the products_id attribute. When i query the database like so:

qry =
session.query(Products_description).join(Products).filter(Products_descri

ption.products_id == :someniceproducts_id)

with some nice existing products_id

qry.count() results in 2L (like i would expect, there being two
description records in the db for the product)

but when i issue:
qry.all()

i only get one object as a result.

I clearly - after studying the docs - don't understand sqlalchemy at
all :)
What am i doing wrong? it looks so easy and straightforward that i
find myself checking my chair twice to be sure it's there before sitting down (..).

count() is going to count rows, not necessarily distinct entities. You have a join in your query so you're going to get multiple Products_description rows with the same identifier, for each related Products. all() gets two rows as well internally, which you'll see if you enable echo='debug' on your engine, but because they have the same primary key identifier for Products_description they are "uniqueified" before being returned.

If you queried for both, query(Products_description, Products), the "uniquifying" step is removed and you'd get two entries.








Can somebody please HELP me :)
Cheers
Freddy

--
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
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 [email protected].
To unsubscribe from this group, send email to [email protected]. 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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to