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
67 lines (52 loc) · 2.08 KB
/
Copy pathmodels.py
File metadata and controls
67 lines (52 loc) · 2.08 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 sqlalchemy import ForeignKey, Table, Column, Integer, String, MetaData, create_engine
from sqlalchemy.orm import relationship, backref
from sqlalchemy.ext.declarative import declarative_base
# Define the naming convention for foreign keys
convention = {
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
}
# Create the metadata object with the naming convention
metadata = MetaData(naming_convention=convention)
# Create the base class for declarative models
Base = declarative_base(metadata=metadata)
# Define the many-to-many relationship table
company_dev = Table(
'company_dev',
Base.metadata,
Column('company_id', ForeignKey('companies.id'), primary_key=True),
Column('dev_id', ForeignKey('devs.id'), primary_key=True),
extend_existing=True,
)
# Define the Company model
class Company(Base):
__tablename__ = 'companies'
id = Column(Integer(), primary_key=True)
name = Column(String())
founding_year = Column(Integer())
Freebie = relationship("Freebie", backref=backref("company"))
devs = relationship("Dev", secondary=company_dev, back_populates="companies")
def __repr__(self):
return f'Company {self.name}, Founding year {self.founding_year}'
# Define the Dev model
class Dev(Base):
__tablename__ = 'devs'
id = Column(Integer(), primary_key=True)
name = Column(String())
Freebie = relationship("Freebie", backref=backref("dev"))
companies = relationship("Company", secondary=company_dev, back_populates="devs")
def __repr__(self):
return f'<Dev {self.name}>'
# Define the Freebie model
class Freebie(Base):
__tablename__ = "Freebie"
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"))
def __repr__(self):
return f'<Freebie {self.item_name}>'
# Create the engine and metadata objects
engine = create_engine('sqlite:///freebies.db')
# Create the tables in the database
Base.metadata.create_all(engine)