Created
July 5, 2016 20:13
-
-
Save jcrudy/fb30f079dfcf29fc6e3bd97b037eee1a to your computer and use it in GitHub Desktop.
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.ext.automap import automap_base | |
from sqlalchemy import create_engine, MetaData, Column, String, Integer | |
import pickle | |
from sqlalchemy.sql.schema import ForeignKey | |
# Create some tables in the database | |
engine = create_engine('sqlite://') | |
engine.execute('CREATE TABLE user (id INTEGER, name TEXT, favorite_color TEXT)') | |
engine.execute('CREATE TABLE profile (id INTEGER, userid INTEGER, summary TEXT)') | |
# Create a metadata based on the database | |
metadata = MetaData() | |
metadata.reflect(bind=engine) | |
# Pickle and unpickle the metadata | |
metadata = pickle.loads(pickle.dumps(metadata)) | |
# Attempt to automap some classes and override some existing columns | |
Base = automap_base(metadata=metadata) | |
class User(Base): | |
__tablename__ = 'user' | |
id = Column('id', Integer, primary_key=True) | |
name = Column('name', String) | |
class Profile(Base): | |
__tablename__ = 'profile' | |
id = Column('id', Integer, primary_key=True) | |
userid = Column('userid', ForeignKey('user.id')) | |
Base.prepare() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment