generated from nicocarcamo/base-workspace-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
106 lines (86 loc) · 2.68 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
99
100
101
102
103
104
105
106
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(120), nullable=False)
lastname = db.Column(db.String(120), nullable=False)
email = db.Column(db.String(100))
password = db.Column(db.String(100), nullable=False)
def serialize(self):
return {
"id": self.id,
"name": self.name,
"lastname": self.lastname,
"email": self.email,
"password": self.password
}
def save(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 Character(db.Model):
__tablename__ = 'characters'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200), nullable=False)
age = db.Column(db.Integer, nullable=False)
def serialize(self):
return {
'id': self.id,
'name': self.name,
'age': self.age
}
def save(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 Planet(db.Model):
__tablename__ = 'planets'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200), nullable=False)
climate = db.Column(db.String(200), nullable=False)
terrain = db.Column(db.String(200), nullable=False)
def serialize(self):
return {
'id': self.id,
'name': self.name,
'climate': self.climate,
'terrain': self.terrain
}
def save(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 Favorite(db.Model):
__tablename__ = 'favorites'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
character_id = db.Column(db.Integer, db.ForeignKey('characters.id'))
planet_id = db.Column(db.Integer, db.ForeignKey('planets.id'))
def serialize(self):
return {
'id': self.id,
'user_id': self.user_id,
'character_id': self.character_id,
'planet_id': self.planet_id
}
def save(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()