Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alfin #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 33 additions & 21 deletions src/models.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
import os
import sys
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy import Column, ForeignKey, Integer, String, Table
from sqlalchemy.orm import relationship, declarative_base
from sqlalchemy import create_engine
from eralchemy2 import render_er

Base = declarative_base()

class Person(Base):
__tablename__ = 'person'
# Here we define columns for the table person
# Notice that each column is also a normal Python instance attribute.
id = Column(Integer, primary_key=True)
name = Column(String(250), nullable=False)

class Address(Base):
__tablename__ = 'address'
# Here we define columns for the table address.
# Notice that each column is also a normal Python instance attribute.
id = Column(Integer, primary_key=True)
street_name = Column(String(250))
street_number = Column(String(250))
post_code = Column(String(250), nullable=False)
person_id = Column(Integer, ForeignKey('person.id'))
person = relationship(Person)

def to_dict(self):
return {}
class User(Base):
__tablename__ = 'user'
ID = Column(Integer, primary_key=True)
username = Column(String(250), nullable=False, unique=True)
firstname = Column(String(250), nullable=True)
lastname = Column(String(250), nullable=False)
password = Column(String(250), nullable=False)

class Planet(Base):
__tablename__ = 'planet'
ID = Column(Integer, primary_key=True)
name = Column(String(250), nullable=False, unique=True)
description = Column(String(500))

class Character(Base):
__tablename__ = 'character'
ID = Column(Integer, primary_key=True)
name = Column(String(250), nullable=False, unique=True)
description = Column(String(500))

user_planet_association = Table('user_planet', Base.metadata,
Column('user_id', Integer, ForeignKey('user.ID')),
Column('planet_id', Integer, ForeignKey('planet.ID'))
)

user_character_association = Table('user_character', Base.metadata,
Column('user_id', Integer, ForeignKey('user.ID')),
Column('character_id', Integer, ForeignKey('character.ID'))
)

User.favorite_planets = relationship('Planet', secondary=user_planet_association, backref="users")
User.favorite_characters = relationship('Character', secondary=user_character_association, backref="users")

## Draw from SQLAlchemy base
render_er(Base, 'diagram.png')