Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One-to-many 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"

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!

Thanks ahead of time.

like image 343
Sunny Malotrha Avatar asked Aug 19 '14 03:08

Sunny Malotrha


People also ask

How do you make a one-to-many relationship in flask?

The comments class attribute defines a One-to-Many relationship between the Post model and the Comment model. You use the db. relationship() method, passing it the name of the comments model ( Comment in this case). You use the backref parameter to add a back reference that behaves like a column to the Comment model.

What is flask relationship?

Relationships are expressed with the relationship() function. However the foreign key has to be separately declared with the ForeignKey class: class Person(db. Model): id = db. Column(db.

What is many to many relationship in SQLAlchemy?

Many to Many relationship between two tables is achieved by adding an association table such that it has two foreign keys - one from each table's primary key.

How do I create a one-to-many database?

A one-to-many relationship in a database occurs when each record in Table A may have many linked records in Table B, but each record in Table B may have only one corresponding record in Table A. A one-to-many relationship in a database is the most common relational database design and is at the heart of good design.


1 Answers

Well, I think you miss the characters relations in the movie + the insert was not totaly right.

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.

For example you can do that:

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

Anyway, without changing this details, you can try this:

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

Inserting

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()
like image 63
Dragu Avatar answered Sep 25 '22 22:09

Dragu