-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
67 lines (54 loc) · 1.8 KB
/
models.py
File metadata and controls
67 lines (54 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from flask_sqlalchemy import SQLAlchemy
import os
db = SQLAlchemy()
def setup_db(app):
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ['DATABASE_URL']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.app = app
db.init_app(app)
class Movie(db.Model):
#this is the movie table in my database . It will have a one to many relationship with the actors table since there are many actors to one movie
__tablename__ = 'movies'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String)
release_date = db.Column(db.Date)
actors = db.relationship('Actor', backref='movies')
def format(self):
return{
'id': self.id,
'title': self.title,
'release_date': self.release_date,
'actors': self.actors
}
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
class Actor(db.Model):
#this would be the actors table. It will be the child of the Movie table
__tablename__ = 'actors'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
age = db.Column(db.Integer)
gender = db.Column(db.String)
movie_id = db.Column(db.Integer, db.ForeignKey('movies.id'), nullable=False)
def format(self):
return {
'id': self.id,
'name': self.name,
'age': self.age,
'gender': self.gender,
'movie_id': self.movie_id
}
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()