Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Miscellaneous
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
practice problem folder
Miscellaneous
Commits
cfa97c66
Commit
cfa97c66
authored
1 month ago
by
Duncan Holmes
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
6f3fd2e9
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lab on ORM/movies.py
+65
-0
65 additions, 0 deletions
lab on ORM/movies.py
with
65 additions
and
0 deletions
lab on ORM/movies.py
0 → 100644
+
65
−
0
View file @
cfa97c66
from
sqlalchemy
import
create_engine
,
Column
,
Integer
,
String
,
Date
,
ForeignKey
from
sqlalchemy.ext.declarative
import
declarative_base
from
sqlalchemy.orm
import
sessionmaker
,
relationship
Persisted
=
declarative_base
()
class
Movie
(
Persisted
):
__tablename__
=
'
movies
'
movie_id
=
Column
(
Integer
,
primary_key
=
True
)
title
=
Column
(
String
(
256
),
nullable
=
False
)
budget
=
Column
(
Integer
)
gross_revenue
=
Column
(
Integer
)
opening_date
=
Column
(
Date
)
#reltionship means that a table is communicating withi another
#the use list means that SQL Alehcmy will return a list of things
#secondary means there's a junciton table involved
#back_populates just means the rest of the table squirsm with eveyrthing else. updates
genres
=
relationship
(
'
Genre
'
,
uselist
=
True
,
secondary
=
'
movie_genres
'
,
back_populates
=
'
movies
'
)
reviews
=
relationship
(
'
Review
'
,
uselist
=
True
,
back_populates
=
'
movie
'
)
class
Review
(
Persisted
):
__tablename__
=
'
reviews
'
review_id
=
Column
(
Integer
,
primary_key
=
True
)
movie_id
=
Column
(
Integer
,
ForeignKey
(
'
movies.movie_id
'
))
movie
=
relationship
(
'
Movie
'
,
back_populates
=
'
reviews
'
)
score
=
Column
(
Integer
)
comments
=
Column
(
String
(
1024
))
class
Genre
(
Persisted
):
__tablename__
=
'
genres
'
genre_id
=
Column
(
Integer
,
primary_key
=
True
)
name
=
Column
(
String
(
256
),
nullable
=
False
)
movies
=
relationship
(
'
Movie
'
,
uselist
=
True
,
secondary
=
'
movie_genres
'
,
back_populates
=
'
genres
'
)
class
MovieGenre
(
Persisted
):
__tablename__
=
'
movie_genres
'
movie_id
=
Column
(
Integer
,
ForeignKey
(
'
movies.movie_id
'
),
primary_key
=
True
)
genre_id
=
Column
(
Integer
,
ForeignKey
(
'
genres.genre_id
'
),
primary_key
=
True
)
votes
=
Column
(
Integer
)
class
MovieDatabase
(
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
()
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