8000 Adding startships orm script · neotycoder/python_orm@b55e929 · GitHub
[go: up one dir, main page]

Skip to content

Commit b55e929

Browse files
committed
Adding startships orm script
1 parent 759f2e9 commit b55e929

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

starships_orm.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Descriptions: This is an example of how to create a ORM.
2+
# This database will house 2 tables that will contain entries for
3+
# both Federation and Klingon starships.
4+
# I though this would be a cool way to show an example of how to
5+
# create an ORM.
6+
7+
# This is just the ORM. Another script will be written to write
8+
# content to this database to inialize the databsase.
9+
# Another script will be written to query the database.
10+
11+
# Import the requirements to build the ORM
12+
import os
13+
import sys
14+
from sqlalchemy import Column, ForeignKey, Integer, String
15+
from sqlalchemy.ext.declarative import declarative_base
16+
from sqlalchemy.orm import relationship
17+
from sqlalchemy import create_engine
18+
19+
# Declare the Star Trek Database
20+
STBase = declarative_base()
21+
22+
# This creates the federation starships database.
23+
class fed_starships(STBase):
24+
__tablename__ = 'fed_starships'
25+
26+
id = Column(Integer, primary_key=True)
27+
# Example: USS Enterprise
28+
hullname = Column(String(250), nullable=False)
29+
# Example: NCC-1701
30+
# Example: NX-01
31+
registry = Column(String(250), nullable=False)
32+
description = Column(String(250), nullable=True)
33+
34+
# This creates the klingon starships database.
35+
class klingon_starships(STBase):
36+
__tablename__ = 'klingons_starships'
37+
38+
id = Column(Integer, primary_key=True)
39+
# Example: USS Enterprise
40+
hullname = Column(String(250), nullable=False)
41+
# Example: NCC-1701
42+
# Example: NX-01
43+
registry = Column(String(250), nullable=True)
44+
description = Column(String(250), nullable=True)
45+
46+
# Define the database engine that STBase will be using.
47+
engine = create_engine('sqlite:///starships.db')
48+
49+
# Create all the tables for the Star Trek Database
50+
# Write all the content for the new tables to this database.
51+
STBase.metadata.create_all(engine)

0 commit comments

Comments
 (0)
0