首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python / SQLAlchemy:如何删除分配表中的记录?

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

Stack Overflow用户
提问于 2018-07-31 21:09:49
回答 1查看 40关注 0票数 0

下面我给你带来了一个可执行的程序。这个程序中有一些注释,以使情况更容易理解。请阅读评论。那我想要什么?我希望程序只删除/移除分配表(Allocation_Film_Genre)中的记录,而不是像电影或流派这样的表中的行。正如您所看到的,在示例中,我将电影"Saw“分配给了类型"Comedy”。一个(故意的)错误。现在我想要解析这个星座,但我不想从数据库中删除"comedy“和"Saw”,而只想删除赋值。但是怎么做呢?

代码语言:javascript
复制
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.
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51614196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档