Hello Michael,

thank you (all) for sqlalchemy :)

The "uniquifying" had me spooked, and i am afraid it got me confused completely about connections, sessions, autoflushing and all the wonderful toys. Still don't know what a good pattern would be. I now even flush() and commit() directly after every add ... (not that it helped, ofc). So, lots of gratitude here for answering me on a sunday evening (for me). Ppl here all of a sudden thought i was going nuts.

Back to it, thank you very much.

cheers


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