Created
October 22, 2016 03:42
-
-
Save marcoscastro/79e8c53c1b7e45c47f88125a63bf6e7c to your computer and use it in GitHub Desktop.
Example foreign key with bottle-sqlalchemy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import Column, DateTime, String, Integer, ForeignKey, func | |
from sqlalchemy.orm import relationship, backref | |
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy import create_engine | |
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) | |
# Create an engine that stores data in the local directory's | |
# sqlalchemy_example.db file. | |
engine = create_engine('sqlite:///exemplo.db') | |
# Create all tables in the engine. This is equivalent to "Create Table" | |
# statements in raw SQL. | |
Base.metadata.create_all(engine) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment