-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
98 lines (77 loc) · 2.82 KB
/
models.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os
from sqlalchemy import Column, String, Integer, create_engine
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import relationship, backref
import json
database_path = "postgres+psycopg2://baftfehglspnqq:473bde560480bb01105756fbf4e19a634897bee74ec326f4a485d54b4bcbfda6@ec2-54-224-124-241.compute-1.amazonaws.com: 5432/d4dl7bdg09o6l5"
# postgres: // baftfehglspnqq: 473bde560480bb01105756fbf4e19a634897bee74ec326f4a485d54b4bcbfda6@ec2-54-224-124-241.compute-1.amazonaws.com: 5432/d4dl7bdg09o6l5
db = SQLAlchemy()
'''
setup_db(app)
binds a flask application and a SQLAlchemy service
'''
def setup_db(app, database_path=database_path):
app.config["SQLALCHEMY_DATABASE_URI"] = database_path
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = app
db.init_app(app)
db.create_all()
# class Movies_Actors(db.Model):
# __tablename__ = 'movies_actors'
# id = db.Column(db.Integer, primary_key=True)
# movie_id = db.Column(db.Integer, db.ForeignKey('movies.id'))
# actor_id = db.Column(db.Integer, db.ForeignKey('actors.id'))
# movie = relationship(Movie, backref=backref("movies_actors", cascade="all, delete-orphan"))
# actor = relationship(Actor, backref=backref("movies_actors", cascade="all, delete-orphan"))
# movies_actors = db.Table("movies_actors",
# Column('id', Integer, primary_key=True),
# Column("actor_id", db.Integer, db.ForeignKey("actor.actor_id")),
# Column("movie_id", db.Integer, db.ForeignKey("movie.movie_id"))
# )
class Movie(db.Model):
__tablename__ = "movie"
movie_id = Column(Integer, primary_key=True)
movie_title = Column(String)
release_date = Column(String)
def __init__(self, title, release_date):
self.title = title
self.release_date = release_date
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()
def format(self):
return {
'id': self.id,
'movie_title': self.movie_title,
'release_date': self.release_date,
}
class Actor(db.Model):
__tablename__ = "actor"
actor_id = Column(Integer, primary_key=True)
name = Column(String)
age = Column(Integer)
gender = Column(String)
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
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()
def format(self):
return {
'id': self.id,
'name': self.name,
'age': self.age,
'gender': self.gender,
}