On Mon, Sep 2, 2013 at 4:01 PM, lars van gemerden <l...@rational-it.com> 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 the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
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