Hello,

Sorry for this very simple question. I have two tables with a
ManyToMany relation between them. Sometimes I need to refresh the
database so I delete elements from both tables. However relations
between deleted rows are still stored inside the automatically created
intermediary table.

I have tried a dozen of variations of "cascade = all", "on delete
cascade", without success.

To clarify the problem, here is a small code:

###############################
# -*- coding: utf-8 -*-
from elixir import *

metadata.bind = "sqlite:///test.db"
metadata.bind.echo = True


class A(Entity):
    name = Field(Unicode(128))
    blist = ManyToMany("B")

class B(Entity):
    name = Field(Unicode(128))
    alist = ManyToMany("A")



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()

################################

A dump of the sqlite database now contains:

PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE __main___a (
        id INTEGER NOT NULL,
        name VARCHAR(128),
        PRIMARY KEY (id)
);
CREATE TABLE __main___b (
        id INTEGER NOT NULL,
        name VARCHAR(128),
        PRIMARY KEY (id)
);
CREATE TABLE __main___b_alist____main___a_blist (
        __main___a_id INTEGER NOT NULL,
        __main___b_id INTEGER NOT NULL,
        PRIMARY KEY (__main___a_id, __main___b_id),
        CONSTRAINT __main___a_blist_fk FOREIGN KEY(__main___a_id)
REFERENCES __main___a (id),
        CONSTRAINT __main___b_alist_fk FOREIGN KEY(__main___b_id)
REFERENCES __main___b (id)
);
INSERT INTO "__main___b_alist____main___a_blist" VALUES(1,1);
COMMIT;



I would like "__main___b_alist____main___a_blist" to be emptied either
when a1 or b1 is deleted.

Thanks !


-- 
You received this message because you are subscribed to the Google Groups 
"SQLElixir" 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/sqlelixir?hl=en.

Reply via email to