[英]Python / SQLAlchemy: How to delete records IN allocation tables?
下面,我為您帶來了一個可執行程序。 該程序中有一些注釋,使情況更易於理解。 請閱讀評論。 好吧,我想要什么? 我希望porgram僅刪除/刪除分配表( Allocation_Film_Genre )中的記錄,而不希望刪除或刪除Film或Genre等表中的行。 如您所見,在示例中,我將電影“ Saw”分配給類型“ Comedy”。 (故意的)錯誤。 現在,我想解析這個星座,但是我不想從數據庫中刪除“喜劇”和“鋸”,而只是刪除分配。 但是如何?
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship, backref
sqlite_url = 'sqlite:///test.sqlite'
engine = sqlalchemy.create_engine(sqlite_url, echo = True)
Base = declarative_base()
class Allocation_Film_Genre(Base):
__tablename__ = 'allocation_film_genre'
genre_id = Column(Integer, ForeignKey('genre.id'), primary_key=True)
film_id = Column(Integer, ForeignKey('film.id'), primary_key=True)
genre = relationship("Genre", backref=backref("allocation_film_genre", lazy='dynamic', cascade="all, delete-orphan" ))
film = relationship("Film", backref=backref("allocation_film_genre", lazy='dynamic', cascade="all, delete-orphan" ))
class Film(Base):
__tablename__ = 'film'
id = Column(Integer, primary_key=True, unique=True)
name = Column(String(80))
class Genre(Base):
__tablename__ = 'genre'
id = Column(Integer, primary_key=True, unique=True)
name = Column(String(80))
# Let us create all tables with certain columns
Base.metadata.create_all(engine)
# Now we have to create a session to work with.
Session = sessionmaker(bind=engine)
session = Session()
# We want to save some movies
film1 = Film(name="Saw")
film2 = Film(name="Amageddon")
film3 = Film(name="Little Stuart")
film4 = Film(name="Doom Day")
session.add_all([film1, film2, film3, film4])
session.commit()
# By the way we also want to save some genres
genre1 = Genre( name = "Horror")
genre2 = Genre( name = "Comedy")
genre3 = Genre( name = "Psycho")
genre4 = Genre( name = "Thriller")
session.add_all([genre1, genre2, genre3, genre4])
session.commit()
# Hold on, we know we created an allocation table, because
# one movie can contains one or more genre, otherwise, one genre
# also can contains one or more movie, right? Let take us a look.
# For simulate we use the movie named 'Saw".
film_obj1 = session.query(Film).filter(Film.name=="Saw").one()
genre_obj1 = session.query(Genre).filter(Genre.name=="Horror").one()
film_obj2 = session.query(Film).filter(Film.name=="Saw").one()
genre_obj2 = session.query(Genre).filter(Genre.name=="Psycho").one()
film_obj3 = session.query(Film).filter(Film.name=="Saw").one()
genre_obj3 = session.query(Genre).filter(Genre.name=="Comedy").one()
allocation1 = Allocation_Film_Genre(film=film_obj1, genre=genre_obj1)
allocation2 = Allocation_Film_Genre(film=film_obj2, genre=genre_obj2)
allocation3 = Allocation_Film_Genre(film=film_obj3, genre=genre_obj3)
session.add_all([allocation1, allocation2, allocation3])
session.commit()
# Ok, we are done. Alle movies and genre are saved, and we also saved all
# allocation records. But wait! There is a mistake. Saw isn't a comedy. Damn!
# Shame on me!
# And now, I don't know what I have to do.
from sqlalchemy import and_
allocation_obj_to_delete _list=session.query(Allocation_Film_Genre).join(Film).join(Genre).filter(and_(Film.name=='Saw',Genre.name=="Comedy")).all()
for obj in allocation_obj_to_delete:
session.delete(obj)
session.commit()
加入查詢並刪除對象
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.