Forked from jcrudy/gist:fb30f079dfcf29fc6e3bd97b037eee1a
Created
February 15, 2022 12:18
-
-
Save malefs/d542b61558e0637ff699bc9588154325 to your computer and use it in GitHub Desktop.
Revisions
-
jcrudy created this gist
Jul 5, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,32 @@ 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()