python - Deleting rows in a ManyToMany intermediate table -


I have too many connections between those two tables. Sometimes I need to refresh the database, so I remove the elements of both tables. However, the links between the deleted rows are still stored in the automatically created arbitrator table.

To illustrate the problem, here is a small code:

  from nectar import * metadata.bind = "Sqlite: ///test.db" metadata. Bind.echo = True options_defults [TrueNote] = True Class A (unit): name = field (Unicode (128)) blist = ManyToMany ("B", casted = 'all, delete, delete-orphans') class B (Unit): name = field (Unicode (128)) alist = manyToMany ("A", cascade = 'all, delete, delete-orphan') setup_all () create_all () A1 = A () a1.name = u " John "b1 = b () b1.name = u" blue "a1.blist.append (b1) session.commit () session.query (a) .delete () Session.query (B) .delete () session. Commit ()  

The dump of the sqlite database is now Does not have:

  sqlite & gt; .dump PRAGMA alien_keys = closed; BEGIN transfer; Create a tab (ID Integer Faucet, Name VARCHAR (128), Primary Key (ID)); Create table (id integer tap, name VARCHAR (128), primary key (id)); Make the table b_alist__a_blist (A_ID is not perfect, b_id pomegranate, primary key (A_ID, b_id), contract a_blist_fac foreign key (aid) reference (id), contract b_alist_fac forward key (BIID) reference reference (id)); INSERT "b_alist__a_blist" values ​​(1,1); COMMIT;  

I would like to empty the "b_alist__a_blist" table, when a1 or b1 is deleted.

Is this possible without using the DELETE statement that are not always supported, SQLite?

Since I am using many relationships with Elixir alone, the solution to this problem is probably trivial.

Skylchemic warnings are generated in the above code:

sqlalchemy / orm / properties.py: 842: On the B-list, delete-orphan cascade is not supported, multi-to-many or many-to-one relationships if not single-parent is set. Single_path = set the true (on relationship). Self._determine_direction ()

This is just because I am trying to add many more Cascade options in this regard. It should be an indication that de-orphans are not the right choice.

I think I got the answer. First of all, The problem is with sqlalchemy alone

Then, this is when it uses syntax:

  session. Query (B) .delete ()  

But anyone can get the desired behavior by using:

  session.delete (b) #where B, b  

session.query (). Delete ()

Code> and sessions .delete () ...


Comments