the method of doing this is quite inefficient (first I'd use query.update(), 
then I wouldn't be doing this at all I'd find some better way to sort the items 
as desired on the query side such as by id descending), but query.all() will 
autoflush the session, so the objects are no longer in .new by the time you 
access them.   A full test passes fine:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base= declarative_base()

class A(Base):
    __tablename__ = "a"

    id = Column(Integer, primary_key=True)
    counter = Column(Integer)

    @classmethod
    def add_object(cls, session):
        obj = A() 
        obj.counter = 0 
        previous_As = session.query(A).all()
        for previous_A in previous_As:
            previous_A.counter += 1
        session.add(obj)

    @classmethod
    def add_objects(cls, session, how_many):
        for i in range(0, how_many):
            cls.add_object(session)

e = create_engine('sqlite://', echo=True)
Base.metadata.create_all(e)

s = Session(e)
A.add_objects(s, 5)
s.commit()

assert \
    s.query(A.id, A.counter).order_by(A.id).all() == \
    [(1, 4), (2, 3),(3, 2), (4, 1), (5, 0)]




On Oct 25, 2011, at 4:40 AM, Andrei Chirila wrote:

> Hello,
> 
> I'm working with a class that creates more objects inside a transaction. The 
> problem I encounter is that when I query the session, I get only what's 
> stored already in the database and I don't match objects in session.new 
> (which were just created).
> Is there any way of running the same filter I use for query on session.new 
> objects (or any other way of retrieving the newly created objects that match 
> my filter?)
> 
> class A(object):
> 
>     @classmethod
>     def add_object(session):
>         obj = A() 
>         a.counter = 0 
>         previous_As = session.query(A).all()
>         for previous_A in previous_As:
>             previous_A.counter += 1
>         session.add(A)
> 
>     @classmethod
>     def add_objects(cls, session, how_many):
>         for i in range(0, how_many):
>             cls.add_object(session)
> 
> In the example, I would call A.add_objects(session, 5), and would expect, in 
> the end, to have 5 objects with counters 4, 3, 2, 1, 0.
> 
> Thank you,
> Andrei Chirila
> 
> -- 
> Mobile: +49 151 42647382
> e-Mail: [email protected]
> 
> -- 
> 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