I may be making a mistake but in the following situation in which
there is a many-to-many relationship if the table containing the
relationships has columns in addition to the Foreign Key columns then
trying to delete relationships by doing:

item.keywords = []

does not work. I don't know SQL well enough to know whether you should
not have any other columns in the relationship table but this doesn't
seem like a reasonable constraint. The delete is issued with the
additional column values having values of None but if there is data in
those columns, the delete does not take place.  Perhaps this is an
expected feature but it doesn't quite make sense to me.

The example is below:

----------------------------------------------------------------------------------------------------------------------


item_table = Table('item',metadata,
              Column('id', Integer, primary_key=True),
              Column('name',String(150)),
)

keyword_table = Table('keyword', metadata,
                 Column('id', Integer, primary_key=True),
                 Column('name', String(25), nullable=False),
)

itemkeyword_table = Table('item_keyword', metadata,
                      Column('item_id', Integer,ForeignKey('item.id'),
nullable=False),
                      Column('keyword_id', Integer,
ForeignKey('keyword.id'), nullable=False),
                      Column('flag', Boolean, default=True),  #<--
additional column
)


class Item_a(object):
    def __init__(self, name):
        self.name = name


class Keyword_a(object):
    def __init__(self, name):
        self.name = name

mapper(Keyword, keyword_table, properties = dict(items =
relation(Item, secondary=itemkeyword_table, lazy=False),))
mapper(Item, item_table, properties = dict(keywords =
relation(Keyword, secondary=itemkeyword_table, lazy=False),))

item = Item("test)
keyword = Keyword("key_for_test")
item.keywords.append(keyword)
item.keywords = []
#the above will not remove the corresponding rows in itemkeyword_table because
#there is non-null data in some of the columns


_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to