Without knowing your application it's very difficult to know...

Is there any chance that the second object is already associated with a 
different session? Perhaps from a different thread, or retrieved from a cache 
or something similar?

Producing a standalone test script would help in diagnosing the problem.

Simon


On 2 Sep 2013, at 18:56, Lars van Gemerden <[email protected]> wrote:

> Well, from the message yes, but i am not setting any primary keys manually, 
> so where could the second instance come from?
> 
> CL
> 
> ====================================
> Lars van Gemerden
> [email protected]
> +31 6 26 88 55 39
> ====================================
> 
> On 2 sep. 2013, at 17:39, Simon King <[email protected]> wrote:
> 
>> On Mon, Sep 2, 2013 at 4:01 PM, lars van gemerden <[email protected]> 
>> wrote:
>>> Hi all,
>>> 
>>> I have a one-to-one and many-to-one relationship (with an association table
>>> in the middle; Report.author-assoctable-Person.reports; why the table in the
>>> middle, you might ask, well it's a long story) and i am setting the author
>>> on a specific Report twice to the same value. This gives the following
>>> error:
>>> 
>>>          InvalidRequestError: Can't attach instance <person at 0x36a9350>;
>>> another instance with key (<class 'models.data.classes.person'>, (2,)) is
>>> already present in this session.
>>> 
>>> The first time i set the author, it works and if i change the author it
>>> works as well.
>>> 
>>> This seems i little strange to me, why not be able to set the attribute to
>>> the same value twice?
>>> 
>>> Otherwise i have to check whether the new and old values are equal (which
>>> through the peculiarties of my code is somewhat wastefull)
>>> 
>> 
>> I think the message is actually telling you that you have 2 different
>> instances of your "person" class with the same primary key. SQLAlchemy
>> uses an identity map to ensure that a particular row in the database
>> is only identified by a single instance within a particular session.
>> If you violate that assumption (eg. by loading one instance of
>> "person" from the database, then creating another instance and setting
>> its primary key), you will get errors like this.
>> 
>> Does that sound plausible?
>> 
>> Simon
>> 
>> -- 
>> You received this message because you are subscribed to a topic in the 
>> Google Groups "sqlalchemy" group.
>> To unsubscribe from this topic, visit 
>> https://groups.google.com/d/topic/sqlalchemy/bkm3-nB6d1o/unsubscribe.
>> To unsubscribe from this group and all its topics, 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/groups/opt_out.
> 
> -- 
> 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/groups/opt_out.

-- 
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/groups/opt_out.

Reply via email to