本文整理汇总了Python中sqlalchemy.ext.declarative.declarative_base方法的典型用法代码示例。如果您正苦于以下问题:Python declarative.declarative_base方法的具体用法?Python declarative.declarative_base怎么用?Python declarative.declarative_base使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.ext.declarative
的用法示例。
在下文中一共展示了declarative.declarative_base方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_temporary_table
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 别名]
def test_temporary_table(self):
test_data = u'Hello, World!'
expected = DataFrame({'spam': [test_data]})
Base = declarative.declarative_base()
class Temporary(Base):
__tablename__ = 'temp_test'
__table_args__ = {'prefixes': ['TEMPORARY']}
id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
spam = sqlalchemy.Column(sqlalchemy.Unicode(30), nullable=False)
Session = sa_session.sessionmaker(bind=self.conn)
session = Session()
with session.transaction:
conn = session.connection()
Temporary.__table__.create(conn)
session.add(Temporary(spam=test_data))
session.flush()
df = sql.read_sql_query(
sql=sqlalchemy.select([Temporary.spam]),
con=conn,
)
tm.assert_frame_equal(df, expected)
示例2: _with_register_classes
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 别名]
def _with_register_classes(cls, fn):
cls_registry = cls.classes
class FindFixtureDeclarative(DeclarativeMeta):
def __init__(cls, classname, bases, dict_):
cls_registry[classname] = cls
return DeclarativeMeta.__init__(
cls, classname, bases, dict_)
class DeclarativeBasic(object):
__table_cls__ = schema.Table
_DeclBase = declarative_base(metadata=cls.metadata,
metaclass=FindFixtureDeclarative,
cls=DeclarativeBasic)
cls.DeclarativeBasic = _DeclBase
fn()
if cls.metadata.tables and cls.run_create_tables:
cls.metadata.create_all(config.db)
示例3: upgrade
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 别名]
def upgrade():
"""Apply Set conn_type as non-nullable"""
Base = declarative_base()
class Connection(Base):
__tablename__ = "connection"
id = sa.Column(sa.Integer(), primary_key=True)
conn_id = sa.Column(sa.String(250))
conn_type = sa.Column(sa.String(500))
# Generate run type for existing records
connection = op.get_bind()
sessionmaker = sa.orm.sessionmaker()
session = sessionmaker(bind=connection)
# imap_default was missing it's type, let's fix that up
session.query(Connection).filter_by(conn_id="imap_default", conn_type=None).update(
{Connection.conn_type: "imap"}, synchronize_session=False
)
session.commit()
with op.batch_alter_table("connection", schema=None) as batch_op:
batch_op.alter_column("conn_type", existing_type=sa.VARCHAR(length=500), nullable=False)
示例4: test_no_inflect
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 别名]
def test_no_inflect(metadata):
Table("simple_items", metadata, Column("id", INTEGER, primary_key=True))
assert (
generate_code(metadata, noinflect=True)
== """\
# coding: utf-8
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class SimpleItems(Base):
__tablename__ = 'simple_items'
id = Column(Integer, primary_key=True)
"""
)
示例5: test_table_kwargs
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 别名]
def test_table_kwargs(metadata):
Table("simple_items", metadata, Column("id", INTEGER, primary_key=True), schema="testschema")
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class SimpleItem(Base):
__tablename__ = 'simple_items'
__table_args__ = {'schema': 'testschema'}
id = Column(Integer, primary_key=True)
"""
)
示例6: test_server_default_double_quotes
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 别名]
def test_server_default_double_quotes(metadata):
Table("simple", metadata, Column("id", INTEGER, primary_key=True, server_default=text('nextval("foo")')))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, Integer, text
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class Simple(Base):
__tablename__ = 'simple'
id = Column(Integer, primary_key=True, server_default=text("nextval(\\"foo\\")"))
"""
)
示例7: test_pascal
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 别名]
def test_pascal(metadata):
Table("CustomerAPIPreference", metadata, Column("id", INTEGER, primary_key=True))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class CustomerAPIPreference(Base):
__tablename__ = 'CustomerAPIPreference'
id = Column(Integer, primary_key=True)
"""
)
示例8: test_underscore
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 别名]
def test_underscore(metadata):
Table("customer_api_preference", metadata, Column("id", INTEGER, primary_key=True))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class CustomerApiPreference(Base):
__tablename__ = 'customer_api_preference'
id = Column(Integer, primary_key=True)
"""
)
示例9: test_pascal_multiple_underscore
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 别名]
def test_pascal_multiple_underscore(metadata):
Table("customer_API__Preference", metadata, Column("id", INTEGER, primary_key=True))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class CustomerAPIPreference(Base):
__tablename__ = 'customer_API__Preference'
id = Column(Integer, primary_key=True)
"""
)
示例10: test_metadata_column
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 别名]
def test_metadata_column(metadata):
Table("simple", metadata, Column("id", INTEGER, primary_key=True), Column("metadata", VARCHAR))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class Simple(Base):
__tablename__ = 'simple'
id = Column(Integer, primary_key=True)
metadata_ = Column('metadata', String)
"""
)
示例11: test_mysql_timestamp
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 别名]
def test_mysql_timestamp(metadata):
Table("simple", metadata, Column("id", INTEGER, primary_key=True), Column("timestamp", mysql.TIMESTAMP))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, Integer, TIMESTAMP
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class Simple(Base):
__tablename__ = 'simple'
id = Column(Integer, primary_key=True)
timestamp = Column(TIMESTAMP)
"""
)
示例12: calculateAverageTemperatures
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 别名]
def calculateAverageTemperatures():
Base = declarative_base()
Session = sessionmaker(bind=op.get_bind())
class Sample(Base):
__tablename__ = 'sample'
id = sa.Column(sa.Integer, name="id", primary_key=True)
moveId = sa.Column(sa.Integer, name="move_id", nullable=False)
temperature = sa.Column(sa.Float, name='temperature')
class Move(Base):
__tablename__ = 'move'
id = sa.Column(sa.Integer, name="id", primary_key=True)
temperature_avg = sa.Column(sa.Float, name='temperature_avg')
session = Session()
averageTemperatures = dict(session.query(Sample.moveId, func.avg(Sample.temperature)).group_by(Sample.moveId).filter(Sample.temperature > 0).all())
for move in session.query(Move):
if move.id in averageTemperatures:
move.temperature_avg = averageTemperatures[move.id]
session.commit()
示例13: model_base
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 别名]
def model_base():
"""Override this fixture to return declarative base of your model
http://docs.sqlalchemy.org/en/latest/orm/extensions/declarative/api.html
.. code-block:: python
from sqlalchemy.ext.declarative import declarative_base
class Base(object):
pass
DeclarativeBase = declarative_base(cls=Base)
class User(DeclarativeBase):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
@pytest.fixture(scope='session')
def model_base():
return DeclarativeBase
"""
raise NotImplementedError("Fixture `model_base` has to be overwritten")
示例14: init_database
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 别名]
def init_database():
Base = declarative_base()
class Model(Base):
__tablename__ = 'models'
TS_name = Column(String(250), nullable=False,primary_key=True)
TS_winner_name = Column(String(250), nullable=False)
TS_model = Column(LargeBinary())
TS_model_params = Column(String(250))
TS_metric = Column(Numeric)
TS_update = Column('TS_update', DATETIME, index=False, nullable=False,primary_key=True,default=datetime.datetime.utcnow)
class TS(Base):
__tablename__ = 'timeseries'
TS_name = Column(String(250), nullable=False,primary_key=True)
TS_data = Column(Text())
TS_update = Column('TS_update', DATETIME, index=False, nullable=False,primary_key=True,default=datetime.datetime.utcnow)
DB_NAME = 'sqlite:///Timecop_modelsv1.db'
engine = create_engine(DB_NAME)
#self.__db.echo = True
Base.metadata.create_all(engine)
示例15: _get_declarative_base
# 需要导入模块: from sqlalchemy.ext import declarative [as 别名]
# 或者: from sqlalchemy.ext.declarative import declarative_base [as 别名]
def _get_declarative_base():
try:
if FORCE_AVOID_INTERNAL_PACKAGES:
# TODO(DATAPIPE-1506|abrar): Currently we have
# force_avoid_internal_packages as a means of simulating an absence
# of a yelp's internal package. And all references
# of force_avoid_internal_packages have to be removed from
# schematizer after we have completely ready for open source.
raise ImportError
from yelp_conn.session import declarative_base
except ImportError:
from sqlalchemy.ext.declarative import declarative_base
return declarative_base()
# The common declarative base used by every data model.