0

I have a junction table which holds references to other two tables. What I want is to remove this refrence from the junction table. In the junction we can have more then one item from a geven type for one plane. The problem is that when I remove a given item all items from the same type are moved.

How I can remove only one item from the junction table?

Here is my database structure.

class Item(object):
    pass

class Plane(object):
    pass

class PlaneItem(object):
    pass    

planeMeta = Table("planes",
    Column("plane_id", Integer, primary_key = True)
)

itemMeta = Table("items",
    Column("item_id", Integer, primary_key = True)
)

planeItem = Table("planes_items",
    Column("planes_items_id", Integer, primary_key = True)
    Column("plane_id", Integer, ForeinKey("planes.plane_id"))
    Column("item_id", Integer, ForeinKey("items.item_id"))
)

mapper(Plane, planeMeta, properties={
    "items": relationship(Item, planeItem, lazy="dynamic")
})

Adding a new Item.

plane = dbSess.query(Plane).filter(Plane.plane_id == my_plane_id).one()
newItem = dbSess.query(Item).filter(Item.item_id == my_new_item).one()

plane.items.append(newItem)

Removing an item.

itemForRemoving = myPlane.items.filter(item_id==4)

myPlane.items.remove(itemForRemoving)
bozhidarc
  • 834
  • 1
  • 11
  • 25

1 Answers1

1

I've recently clarified the documentation for this question that's been coming up a lot lately.

The removal of the item is all SQLAlchemy needs in order to know to remove a row from the "secondary" table. It only removes one row per removal, so the "all items from the same type are moved" must be some other usage you're not illustrating here.

See http://docs.sqlalchemy.org/en/rel_0_7/orm/relationships.html#deleting-rows-from-the-many-to-many-table.

zzzeek
  • 72,307
  • 23
  • 193
  • 185