本文整理汇总了Python中sqlalchemy.schema.MetaData方法的典型用法代码示例。如果您正苦于以下问题:Python schema.MetaData方法的具体用法?Python schema.MetaData怎么用?Python schema.MetaData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.schema
的用法示例。
在下文中一共展示了schema.MetaData方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: provide_metadata
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import MetaData [as 别名]
def provide_metadata(fn, *args, **kw):
"""Provide bound MetaData for a single test, dropping afterwards."""
from . import config
from . import engines
from sqlalchemy import schema
metadata = schema.MetaData(config.db)
self = args[0]
prev_meta = getattr(self, 'metadata', None)
self.metadata = metadata
try:
return fn(*args, **kw)
finally:
engines.drop_all_tables(metadata, config.db)
self.metadata = prev_meta
示例2: _create_table_setup
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import MetaData [as 别名]
def _create_table_setup(self):
from sqlalchemy import Table, Column, PrimaryKeyConstraint
column_names_and_types = \
self._get_column_names_and_types(self._sqlalchemy_type)
columns = [Column(name, typ, index=is_index)
for name, typ, is_index in column_names_and_types]
if self.keys is not None:
if not is_list_like(self.keys):
keys = [self.keys]
else:
keys = self.keys
pkc = PrimaryKeyConstraint(*keys, name=self.name + '_pk')
columns.append(pkc)
schema = self.schema or self.pd_sql.meta.schema
# At this point, attach to new metadata, only attach to self.meta
# once table is created.
from sqlalchemy.schema import MetaData
meta = MetaData(self.pd_sql, schema=schema)
return Table(self.name, meta, *columns, schema=schema)
示例3: codegen
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import MetaData [as 别名]
def codegen(args):
# Use reflection to fill in the metadata
engine = create_engine(args.url)
metadata = MetaData(engine)
tables = args.tables.split(",") if args.tables else None
metadata.reflect(engine, args.schema, not args.noviews, tables)
if db.session.bind.dialect.name == "sqlite":
# dirty hack for sqlite
engine.execute("""PRAGMA journal_mode = OFF""")
# Write the generated model code to the specified file or standard output
capture = StringIO()
# outfile = io.open(args.outfile, 'w', encoding='utf-8') if args.outfile else capture # sys.stdout
generator = CodeGenerator(metadata, args.noindexes, args.noconstraints, args.nojoined, args.noinflect, args.noclasses)
generator.render(capture)
generated = capture.getvalue()
generated = fix_generated(generated)
if args.outfile:
outfile = io.open(args.outfile, "w", encoding="utf-8")
outfile.write(generated)
return generated
示例4: test_column_adaptation
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import MetaData [as 别名]
def test_column_adaptation(metadata):
Table("simple_items", metadata, Column("id", postgresql.BIGINT), Column("length", postgresql.DOUBLE_PRECISION))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import BigInteger, Column, Float, MetaData, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('id', BigInteger),
Column('length', Float)
)
"""
)
示例5: test_mysql_column_types
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import MetaData [as 别名]
def test_mysql_column_types(metadata):
Table("simple_items", metadata, Column("id", mysql.INTEGER), Column("name", mysql.VARCHAR(255)))
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, Integer, MetaData, String, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('id', Integer),
Column('name', String(255))
)
"""
)
示例6: test_noindexes_table
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import MetaData [as 别名]
def test_noindexes_table(metadata):
simple_items = Table("simple_items", metadata, Column("number", INTEGER), CheckConstraint("number > 2"))
simple_items.indexes.add(Index("idx_number", simple_items.c.number))
assert (
generate_code(metadata, noindexes=True)
== """\
# coding: utf-8
from sqlalchemy import CheckConstraint, Column, Integer, MetaData, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('number', Integer),
CheckConstraint('number > 2')
)
"""
)
示例7: test_noconstraints_table
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import MetaData [as 别名]
def test_noconstraints_table(metadata):
simple_items = Table("simple_items", metadata, Column("number", INTEGER), CheckConstraint("number > 2"))
simple_items.indexes.add(Index("idx_number", simple_items.c.number))
assert (
generate_code(metadata, noconstraints=True)
== """\
# coding: utf-8
from sqlalchemy import Column, Integer, MetaData, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('number', Integer, index=True)
)
"""
)
示例8: test_no_classes
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import MetaData [as 别名]
def test_no_classes(metadata):
Table("simple_items", metadata, Column("id", INTEGER, primary_key=True))
assert (
generate_code(metadata, noclasses=True)
== """\
# coding: utf-8
from sqlalchemy import Column, Integer, MetaData, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('id', Integer, primary_key=True)
)
"""
)
示例9: test_schema_table
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import MetaData [as 别名]
def test_schema_table(metadata):
Table("simple_items", metadata, Column("name", VARCHAR), schema="testschema")
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, MetaData, String, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('name', String),
schema='testschema'
)
"""
)
示例10: test_schema_boolean
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import MetaData [as 别名]
def test_schema_boolean(metadata):
Table(
"simple_items", metadata, Column("bool1", INTEGER), CheckConstraint("testschema.simple_items.bool1 IN (0, 1)"), schema="testschema"
)
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Boolean, Column, MetaData, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('bool1', Boolean),
schema='testschema'
)
"""
)
示例11: test_foreign_key_options
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import MetaData [as 别名]
def test_foreign_key_options(metadata):
Table(
"simple_items",
metadata,
Column(
"name", VARCHAR, ForeignKey("simple_items.name", ondelete="CASCADE", onupdate="CASCADE", deferrable=True, initially="DEFERRED")
),
)
assert (
generate_code(metadata)
== """\
# coding: utf-8
from sqlalchemy import Column, ForeignKey, MetaData, String, Table
metadata = MetaData()
t_simple_items = Table(
'simple_items', metadata,
Column('name', String, ForeignKey('simple_items.name', ondelete='CASCADE', \
onupdate='CASCADE', deferrable=True, initially='DEFERRED'))
)
"""
)
示例12: metadata_fixture
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import MetaData [as 别名]
def metadata_fixture(ddl="function"):
"""Provide MetaData for a pytest fixture."""
from sqlalchemy.testing import config
from . import fixture_functions
def decorate(fn):
def run_ddl(self):
from sqlalchemy import schema
metadata = self.metadata = schema.MetaData()
try:
result = fn(self, metadata)
metadata.create_all(config.db)
# TODO:
# somehow get a per-function dml erase fixture here
yield result
finally:
metadata.drop_all(config.db)
return fixture_functions.fixture(scope=ddl)(run_ddl)
return decorate
示例13: __init__
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import MetaData [as 别名]
def __init__(self):
###
# Private database engine and metadata attributes.
#
self._engine = None
self._metadata = MetaData(schema=db_option('schema'))
###
# Session Factory classes, later initialized in self.initialize() method.
#
# The self.Session corresponds to a session factory that doesn't expire ORM instances from memory
# after getting committed.
#
# The self.OnCommitExpiringSession corresponds to a session factory that expires ORM instances from
# memory after getting committed.
#
self.Session = None
self.OnCommitExpiringSession = None
###
# Declarative Base Model class.
#
self.BaseModel = declarative_base(
cls=BaseModelMixin,
metadata=MetaData(schema=db_option('schema')))
示例14: reflect_table
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import MetaData [as 别名]
def reflect_table(engine, klass):
"""Inspect and reflect objects"""
try:
meta = MetaData()
meta.reflect(bind=engine)
except OperationalError as e:
raise DatabaseError(error=e.orig.args[1], code=e.orig.args[0])
# Try to reflect from any of the supported tables
table = None
for tb in klass.tables():
if tb in meta.tables:
table = meta.tables[tb]
break
if table is None:
raise DatabaseError(error="Invalid schema. Table not found",
code="-1")
# Map table schema into klass
mapper(klass, table,
column_prefix=klass.column_prefix())
return table
示例15: _execute_create
# 需要导入模块: from sqlalchemy import schema [as 别名]
# 或者: from sqlalchemy.schema import MetaData [as 别名]
def _execute_create(self):
# Inserting table into database, add to MetaData object
self.table = self.table.tometadata(self.pd_sql.meta)
self.table.create()