forked from learn-co-curriculum/python-p3-freebie-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
72 lines (51 loc) · 1.82 KB
/
models.py
File metadata and controls
72 lines (51 loc) · 1.82 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
68
69
70
71
72
from sqlalchemy import ForeignKey, Column, Integer, String, MetaData
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
# naming conventions for constraints
convention = {
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
}
metadata = MetaData(naming_convention=convention)
Base = declarative_base(metadata=metadata)
class Company(Base):
__tablename__ = 'companies'
id = Column(Integer(), primary_key=True)
name = Column(String())
founding_year = Column(Integer())
# relationships
freebies=relationship("Freebie", back_populates="company")
devs=relationship(
"Dev",
secondary="freebies",
back_populates="companies",
viewonly=True
)
def __repr__(self):
return f'<Company {self.name}>'
class Dev(Base):
__tablename__ = 'devs'
id = Column(Integer(), primary_key=True)
name= Column(String())
# relationships
freebies=relationship("Freebie", back_populates="dev")
companies=relationship(
"Company",
secondary="freebies",
back_populates="devs",
viewonly=True
)
def __repr__(self):
return f'<Dev {self.name}>'
class Freebie(Base):
__tablename__='freebies'
id =Column(Integer, primary_key=True)
item_name=Column(String)
value=Column(Integer)
company_id=Column(Integer, ForeignKey('companies.id'))
dev_id=Column(Integer, ForeignKey('devs.id'))
company=relationship("Company", back_populates='freebies')
dev=relationship("Dev", back_populates="freebies")
def __repr__(self):
return f'<Freebie {self.item_name} ({self.value})>'
# Here we added Freebie class because the project is called freebie tracker
# Establish relationship between companies, devs and freebies