Re: [sqlalchemy] sqlalchemy.exc.InvalidRequestError: Can't attach instance another instance with key is already present in this session

2021-07-20 Thread Mike Bayer
On Tue, Jul 20, 2021, at 10:00 AM, Evgenii wrote: > As it was mentioned before, I create repeated elements in relationship > deliberately. Assuming this implies the table can have no candidate key, this is an antipattern in SQL and there are lots of answers/articles/etc on the web why all

Re: [sqlalchemy] sqlalchemy.exc.InvalidRequestError: Can't attach instance another instance with key is already present in this session

2021-07-20 Thread Evgenii
As it was mentioned before, I create repeated elements in relationship deliberately. Moreover, alchemy allows me to do that, but it fails during deleting instances and modifying relationships. Unfortunately, this is not that case, where I can start all over again. All examples are maximally

Re: [sqlalchemy] sqlalchemy.exc.InvalidRequestError: Can't attach instance another instance with key is already present in this session

2021-07-20 Thread Mike Bayer
if you are mapping ORM classes to the same table that is also used as the "secondary" table in a relationship() that can lead to the ORM inserting more than one row for that table. based on the name "foo_bar" I would imagine something like this might be going on. > > Pls tell how to

Re: [sqlalchemy] sqlalchemy.exc.InvalidRequestError: Can't attach instance another instance with key is already present in this session

2021-07-20 Thread Evgenii
Mike, thank you for the answer. But I have another problem with deleting the instance. Even though all instances belong to the same session (it is possible to push foo instance): with Session() as session: b1 = session.query(BarTable).get(1) b2 = session.query(BarTable).get(1) foo

Re: [sqlalchemy] sqlalchemy.exc.InvalidRequestError: Can't attach instance another instance with key is already present in this session

2021-07-19 Thread Mike Bayer
This is all expected behavior, the main reason you're having problems is that you are using multiple sessions and mixing their results together.If you need to do this, there are few approaches, the most basic being to use the merge() method:

[sqlalchemy] sqlalchemy.exc.InvalidRequestError: Can't attach instance another instance with key is already present in this session

2021-07-19 Thread Evgenii
Hello! I’m using many-to-many relation, and this relationship bar_list must have list of instances. Some of them can be repeated (ex. [inst1, inst2, inst1]). I attach very simplified code there (all of database interaction is hidden under the hood, user accesses database at top level, but this