Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Database Design Practice
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SOFT Core
SOFT 161 and 162
Database Design Practice
Commits
a80836f1
Commit
a80836f1
authored
3 years ago
by
Shruti Bolman
Browse files
Options
Downloads
Patches
Plain Diff
ORM database design
parent
80b96fd4
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
diet.py
+61
-0
61 additions, 0 deletions
diet.py
with
61 additions
and
0 deletions
diet.py
0 → 100644
+
61
−
0
View file @
a80836f1
from
sqlalchemy
import
create_engine
,
Column
,
Integer
,
String
,
DateTime
,
ForeignKey
from
sqlalchemy.ext.declarative
import
declarative_base
from
sqlalchemy.orm
import
sessionmaker
,
relationship
Persisted
=
declarative_base
()
# pylint: disable=invalid-name
class
User
(
Persisted
):
__tablename__
=
'
users
'
user_id
=
Column
(
Integer
,
primary_key
=
True
)
name
=
Column
(
String
(
256
),
nullable
=
False
)
meals
=
relationship
(
'
Meal
'
,
uselist
=
True
,
back_populates
=
'
user
'
)
class
Meal
(
Persisted
):
__tablename__
=
'
meals
'
meal_id
=
Column
(
Integer
,
primary_key
=
True
)
user_id
=
Column
(
Integer
,
ForeignKey
(
'
users.user_id
'
,
ondelete
=
'
CASCADE
'
),
nullable
=
False
)
timestamp
=
Column
(
DateTime
,
nullable
=
False
)
user
=
relationship
(
'
User
'
,
back_populates
=
'
meals
'
)
meal_food_types
=
relationship
(
'
MealFoodType
'
,
uselist
=
True
,
back_populates
=
'
meal
'
)
food_types
=
relationship
(
'
FoodType
'
,
uselist
=
True
,
secondary
=
'
meal_food_types
'
,
viewonly
=
True
)
class
FoodType
(
Persisted
):
__tablename__
=
'
food_types
'
food_type_id
=
Column
(
Integer
,
primary_key
=
True
)
name
=
Column
(
String
(
256
),
nullable
=
False
)
meal_food_types
=
relationship
(
'
MealFoodType
'
,
uselist
=
True
,
back_populates
=
'
food_type
'
)
meals
=
relationship
(
'
Meal
'
,
uselist
=
True
,
secondary
=
'
meal_food_types
'
,
viewonly
=
True
)
class
MealFoodType
(
Persisted
):
__tablename__
=
'
meal_food_types
'
meal_id
=
Column
(
Integer
,
ForeignKey
(
'
meals.meal_id
'
,
ondelete
=
'
CASCADE
'
),
primary_key
=
True
)
food_type_id
=
Column
(
Integer
,
ForeignKey
(
'
food_types.food_type_id
'
,
ondelete
=
'
CASCADE
'
),
primary_key
=
True
)
amount
=
Column
(
Integer
,
nullable
=
False
)
meal
=
relationship
(
'
Meal
'
,
back_populates
=
'
meal_food_types
'
)
food_type
=
relationship
(
'
FoodType
'
,
back_populates
=
'
meal_food_types
'
)
class
DietDatabase
(
object
):
@staticmethod
def
construct_mysql_url
(
authority
,
port
,
database
,
username
,
password
):
return
f
'
mysql+mysqlconnector://
{
username
}
:
{
password
}
@
{
authority
}
:
{
port
}
/
{
database
}
'
@staticmethod
def
construct_in_memory_url
():
return
'
sqlite:///
'
def
__init__
(
self
,
url
):
self
.
engine
=
create_engine
(
url
)
self
.
Session
=
sessionmaker
()
# pylint: disable=invalid-name
self
.
Session
.
configure
(
bind
=
self
.
engine
)
def
ensure_tables_exist
(
self
):
Persisted
.
metadata
.
create_all
(
self
.
engine
)
def
create_session
(
self
):
return
self
.
Session
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment