繁体   English   中英

Python / SQLAlchemy:如何删除分配表中的记录?

[英]Python / SQLAlchemy: How to delete records IN allocation tables?

下面,我为您带来了一个可执行程序。 该程序中有一些注释,使情况更易于理解。 请阅读评论。 好吧,我想要什么? 我希望porgram仅删除/删除分配表( Allocation_Film_Genre )中的记录,而不希望删除或删除FilmGenre等表中的行。 如您所见,在示例中,我将电影“ 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM