Skip to content

Instantly share code, notes, and snippets.

@fcofdez
Created September 12, 2014 16:35
Show Gist options
  • Save fcofdez/283e672a7b3312a977ef to your computer and use it in GitHub Desktop.
Save fcofdez/283e672a7b3312a977ef to your computer and use it in GitHub Desktop.
sqlalcheym
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)
data = Column(String,
server_default=FetchedValue())
__mapper_args__ = {
"eager_defaults": True # should not be needed
}
# should work whether or not this is set
# __table_args__ = {"implicit_returning": False}
engine = create_engine("postgresql://scott:tiger@localhost/test", echo=True)
Base.metadata.drop_all(engine)
engine.execute("create table a (id serial primary key, data varchar)")
engine.execute("ALTER TABLE a ALTER COLUMN data SET DEFAULT concat(md5(CAST(random() AS TEXT)), '2')")
s = Session(engine)
a1 = A()
s.add(a1)
s.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment