一对多 Flask |SQLAlchemy [英] One-to-many Flask | SQLAlchemy
问题描述
我正在尝试使用 Flask 和 SQLAlchemy 创建一对多关系.
I am trying to create a one-to-many relationship using Flask and SQLAlchemy.
我希望一对多的关系是这样的:
I want the one-to-many relationship to be as so:
对于任何一部电影,可以有多个角色"
"For any single movie, there can be multiple characters"
这是我到目前为止所拥有的,但它现在以一对一的形式保存在我的数据库中.(一部电影对一个角色,多个角色在DB中多次保存)
Here it what I have so far, but it is saving in my DB as one-to-one right now. (One movie to one character, saving multiple times in DB for multiple characters)
class Movie(db.Model):
__tablename__ = "movies"
id = db.Column('movies_id', db.Integer, primary_key=True)
movie_type = db.Column('movie_type', db.Text())
def __init__(self, movie_type):
self.movie_type = movie_type
def __repr__(self):
return '<Movie %r>' % self.id
class Character(db.Model):
__tablename__ = "characters"
id = db.Column('character_id', db.Integer, primary_key=True)
character_description = db.Column('character_description', db.Text())
movie_id = db.Column(db.Integer, db.ForeignKey('movies.movie_id'))
movie = db.relationship('Movie', backref='characters', lazy='dynamic')
def __init__(self, character_description, movie):
self.character_description = character_description
self.movie = movie
def __repr__(self):
return '<Character %r>' % self.id
我像这样保存到数据库中:
I am saving into the DB like this:
movie = models.movie(movie_type)
character = models.Character(character_description, movie)
db.session.add(movie)
db.session.add(character)
db.session.commit()
最终目标是能够查找角色所在的电影.如果您也能帮我解决这个问题,那就太好了!
The end goal is to be able to look up what movie a character is in. If you could also help me out with that query, that would be great!
提前致谢.
推荐答案
好吧,我想你错过了电影中的人物关系 + 插入不完全正确.
Well, I think you miss the characters relations in the movie + the insert was not totaly right.
还有一些小细节你必须小心.为什么电影的id是movieS_id,而角色的id是character_id?
There is also little details that you have to be carefull. Why id of movie is movieS_id and id of character is character_id ?
此外,如果未指定,列名与变量名相同.
Also, the name of the column is the same as the name of the variable if not specified.
例如你可以这样做:
character_description = db.Column(db.Text())
无论如何,不改变这些细节,你可以试试这个:
class Movie(db.Model):
__tablename__ = "movies"
id = db.Column('movies_id', db.Integer, primary_key=True)
movie_type = db.Column('movie_type', db.Text())
characters = db.relationship("Character", backref="movie", lazy='dynamic')
def __init__(self, movie_type):
self.movie_type = movie_type
def __repr__(self):
return '<Movie %r>' % self.id
class Character(db.Model):
__tablename__ = "characters"
id = db.Column('character_id', db.Integer, primary_key=True)
character_description = db.Column('character_description', db.Text())
movie_id = db.Column(db.Integer, db.ForeignKey('movies.movies_id'))
movie = db.relationship('Movie')
def __init__(self, character_description, movie):
self.character_description = character_description
self.movie = movie
def __repr__(self):
return '<Character %r>' % self.id
插入
c = Character(character_description='c')
c2 = Character(character_description='c2')
m = Movie(movie_type ='action')
# link characters to movie
m.characters.append(c)
m.characters.append(c2)
# or
m.characters.extend([c,c2])
db.session.add(m)
# add characters
db.session.add(c)
db.session.add(c2)
# or
db.session.add_all([c,c2])
# commit
db.session.commit()
这篇关于一对多 Flask |SQLAlchemy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!