Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
ORM in Class
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
Container registry
Model registry
Operate
Environments
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
ORM in Class
Commits
37ba36f3
Commit
37ba36f3
authored
1 month ago
by
Shruti Bolman
Browse files
Options
Downloads
Patches
Plain Diff
updated imports and datetime
parent
28572976
Branches
main
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
diner.py
+8
-3
8 additions, 3 deletions
diner.py
diner_installer.py
+10
-9
10 additions, 9 deletions
diner_installer.py
with
18 additions
and
12 deletions
diner.py
+
8
−
3
View file @
37ba36f3
from
sqlalchemy
import
create_engine
,
Column
,
Integer
,
String
,
DateTime
,
ForeignKey
from
sqlalchemy.ext.declarative
import
declarative_base
from
sqlalchemy.orm
import
sessionmaker
,
relationship
from
sqlalchemy.orm
import
declarative_base
,
sessionmaker
,
relationship
# In Python, unlike many languages, it is possible to create new classes at
...
...
@@ -8,13 +7,14 @@ from sqlalchemy.orm import sessionmaker, relationship
# class that we can use for objects that we want SQLAlchemy to persist in a
# database. We then store that class in variable named Persisted; effectively,
# we make a new class called Persisted.
Persisted
=
declarative_base
()
# pylint: disable=invalid-name
Persisted
=
declarative_base
()
class
Menu
(
Persisted
):
__tablename__
=
'
menus
'
menu_id
=
Column
(
Integer
,
primary_key
=
True
)
# primary keys default to auto_increment
name
=
Column
(
String
(
256
),
nullable
=
False
)
# nullable is the default
items
=
relationship
(
'
Item
'
,
uselist
=
True
,
back_populates
=
'
menu
'
)
class
Item
(
Persisted
):
...
...
@@ -24,12 +24,15 @@ class Item(Persisted):
menu_id
=
Column
(
Integer
,
ForeignKey
(
'
menus.menu_id
'
,
ondelete
=
'
CASCADE
'
),
nullable
=
False
)
name
=
Column
(
String
(
256
),
nullable
=
False
)
price
=
Column
(
Integer
,
nullable
=
False
)
menu
=
relationship
(
'
Menu
'
,
back_populates
=
'
items
'
)
order_items
=
relationship
(
'
OrderItem
'
,
uselist
=
True
,
back_populates
=
'
item
'
)
class
Order
(
Persisted
):
__tablename__
=
'
orders
'
order_id
=
Column
(
Integer
,
primary_key
=
True
)
timestamp
=
Column
(
DateTime
)
order_items
=
relationship
(
'
OrderItem
'
,
uselist
=
True
,
back_populates
=
'
order
'
)
class
OrderItem
(
Persisted
):
...
...
@@ -37,6 +40,8 @@ class OrderItem(Persisted):
order_id
=
Column
(
Integer
,
ForeignKey
(
'
orders.order_id
'
,
ondelete
=
'
CASCADE
'
),
primary_key
=
True
)
item_id
=
Column
(
Integer
,
ForeignKey
(
'
items.item_id
'
,
ondelete
=
'
CASCADE
'
),
primary_key
=
True
)
amount
=
Column
(
Integer
)
item
=
relationship
(
'
Item
'
,
back_populates
=
'
order_items
'
)
order
=
relationship
(
'
Order
'
,
back_populates
=
'
order_items
'
)
class
DinerDatabase
(
object
):
...
...
This diff is collapsed.
Click to expand it.
diner_installer.py
+
10
−
9
View file @
37ba36f3
from
datetime
import
datetime
from
sys
import
stderr
from
sqlalchemy.exc
import
SQLAlchemyError
...
...
@@ -15,7 +16,7 @@ def add_starter_data(session):
orange_juice
=
Item
(
menu
=
beverage_menu
,
name
=
'
Orange Juice
'
,
price
=
149
)
short_stack
=
Item
(
menu
=
food_menu
,
name
=
'
Pancakes
'
,
price
=
399
)
regular_pancakes
=
Item
(
menu
=
food_menu
,
name
=
'
Pancakes
'
,
price
=
499
)
first_order
=
Order
(
order_items
=
[
OrderItem
(
item
=
orange_juice
,
amount
=
2
)])
first_order
=
Order
(
order_items
=
[
OrderItem
(
item
=
orange_juice
,
amount
=
2
)]
,
timestamp
=
datetime
.
now
()
)
session
.
add
(
beverage_menu
)
session
.
add
(
food_menu
)
session
.
add
(
orange_juice
)
...
...
@@ -26,17 +27,17 @@ def add_starter_data(session):
def
main
():
try
:
url
=
DinerDatabase
.
construct_mysql_url
(
'
localhost
'
,
3306
,
'
diner
'
,
'
root
'
,
'
cse1208
'
)
url
=
DinerDatabase
.
construct_mysql_url
(
'
localhost
'
,
3306
,
'
diner
'
,
'
root
'
,
'
mysqlpassword
'
)
diner_database
=
DinerDatabase
(
url
)
diner_database
.
ensure_tables_exist
()
print
(
'
Tables created.
'
)
#
session = diner_database.create_session()
#
if already_has_data(session):
#
print('Not creating records because some already exist.')
#
else:
#
add_starter_data(session)
#
session.commit()
#
print('Records created.')
session
=
diner_database
.
create_session
()
if
already_has_data
(
session
):
print
(
'
Not creating records because some already exist.
'
)
else
:
add_starter_data
(
session
)
session
.
commit
()
print
(
'
Records created.
'
)
except
SQLAlchemyError
as
exception
:
print
(
'
Database setup failed!
'
,
file
=
stderr
)
print
(
f
'
Cause:
{
exception
}
'
,
file
=
stderr
)
...
...
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