One-to-Many Bottle | SQLAlchemy

I am trying to create a one-to-many relationship using the checkbox and SQLAlchemy.

I want the one-to-many relationship to be:

"There can be multiple characters for any single movie."

Here it is, that I am still, but it saves in my database as one to one right now. (One movie for one character, storing several times in the database for several 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 save to the database as follows:

 movie = models.movie(movie_type) character = models.Character(character_description, movie) db.session.add(movie) db.session.add(character) db.session.commit() 

The ultimate goal is to see which movie the character is in. If you could help me in this matter, that would be great!

Thanks in advance.

+6
source share
1 answer

Well, I think you missed the relationship with the characters in the movie + the insert was not completely correct.

There are also small details that you must be careful about. Why is the movie id movieS_id and the character id character_id?

In addition, the column name is the same as the variable name if not specified.

For example, you can do this:

 character_description = db.Column(db.Text()) 

In any case, without changing this information, you can try the following:

 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 

Insert

 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() 
+5
source

Source: https://habr.com/ru/post/974013/


All Articles