本文整理汇总了Python中sqlalchemy.ext.automap.automap_base方法的典型用法代码示例。如果您正苦于以下问题:Python automap.automap_base方法的具体用法?Python automap.automap_base怎么用?Python automap.automap_base使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.ext.automap
的用法示例。
在下文中一共展示了automap.automap_base方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _is_many_to_many
# 需要导入模块: from sqlalchemy.ext import automap [as 别名]
# 或者: from sqlalchemy.ext.automap import automap_base [as 别名]
def _is_many_to_many(automap_base, table):
fk_constraints = [const for const in table.constraints
if isinstance(const, ForeignKeyConstraint)]
if len(fk_constraints) != 2:
return None, None, None
cols = sum(
[[fk.parent for fk in fk_constraint.elements]
for fk_constraint in fk_constraints], [])
if set(cols) != set(table.c):
return None, None, None
return (
fk_constraints[0].elements[0].column.table,
fk_constraints[1].elements[0].column.table,
fk_constraints
)
示例2: reflect_hints_db
# 需要导入模块: from sqlalchemy.ext import automap [as 别名]
# 或者: from sqlalchemy.ext.automap import automap_base [as 别名]
def reflect_hints_db(db_path):
"""
Reflect the database schema of the hints database, automapping the existing tables
The NullPool is used to avoid concurrency issues with luigi. Using this activates pooling, but since sqlite doesn't
really support pooling, what effectively happens is just that it locks the database and the other connections wait.
:param db_path: path to hints sqlite database
:return: sqlalchemy.MetaData object, sqlalchemy.orm.Session object
"""
engine = sqlalchemy.create_engine('sqlite:///{}'.format(db_path), poolclass=NullPool)
metadata = sqlalchemy.MetaData()
metadata.reflect(bind=engine)
Base = automap_base(metadata=metadata)
Base.prepare()
speciesnames = Base.classes.speciesnames
seqnames = Base.classes.seqnames
hints = Base.classes.hints
featuretypes = Base.classes.featuretypes
Session = sessionmaker(bind=engine)
session = Session()
return speciesnames, seqnames, hints, featuretypes, session
开发者ID:ComparativeGenomicsToolkit,项目名称:Comparative-Annotation-Toolkit,代码行数:24,代码来源:hintsDatabaseInterface.py
示例3: __init__
# 需要导入模块: from sqlalchemy.ext import automap [as 别名]
# 或者: from sqlalchemy.ext.automap import automap_base [as 别名]
def __init__(self):
Base = automap_base()
engine = create_engine("sqlite:///sqlcell.db")
Base.prepare(engine, reflect=True)
self.classes = Base.classes
self.tables = Base.metadata.tables.keys()
self.Sqlcell = Base.classes.sqlcell
self.Engines = Base.classes.engines
self.Hooks = Base.classes.hooks
Session = sessionmaker(autoflush=False)
Session.configure(bind=engine)
self.session = Session()
dbs = self.session.query(self.Engines).all()
self.db_info = {}
for row in dbs:
engine = row.engine
if row.db:
self.db_info[row.db] = engine
if row.alias:
self.db_info[row.alias] = engine
self.db_info[engine] = engine
self.db_info[row.host] = engine
示例4: _init_db
# 需要导入模块: from sqlalchemy.ext import automap [as 别名]
# 或者: from sqlalchemy.ext.automap import automap_base [as 别名]
def _init_db(self):
"""Initialize the database and automapper."""
self.models = {}
# initialize the DB engine
self.engine = create_engine(self.options["database_url"])
# initialize DB metadata
self.metadata = MetaData()
self.metadata.bind = self.engine
# Create the tables
self._create_tables()
# initialize the automap mapping
self.base = automap_base(bind=self.engine, metadata=self.metadata)
self.base.prepare(self.engine, reflect=True)
# initialize session
self.session = create_session(bind=self.engine, autocommit=False)
示例5: _is_many_to_many
# 需要导入模块: from sqlalchemy.ext import automap [as 别名]
# 或者: from sqlalchemy.ext.automap import automap_base [as 别名]
def _is_many_to_many(automap_base, table):
fk_constraints = [const for const in table.constraints
if isinstance(const, ForeignKeyConstraint)]
if len(fk_constraints) != 2:
return None, None, None
cols = sum(
[[fk.parent for fk in fk_constraint.elements]
for fk_constraint in fk_constraints], [])
#STDM association entities have 3 columns
if len(table.c) > 3:
return None, None, None
return (
fk_constraints[0].elements[0].column.table,
fk_constraints[1].elements[0].column.table,
fk_constraints
)
示例6: _is_many_to_many
# 需要导入模块: from sqlalchemy.ext import automap [as 别名]
# 或者: from sqlalchemy.ext.automap import automap_base [as 别名]
def _is_many_to_many(automap_base, table):
fk_constraints = [
const
for const in table.constraints
if isinstance(const, ForeignKeyConstraint)
]
if len(fk_constraints) != 2:
return None, None, None
cols = sum(
[
[fk.parent for fk in fk_constraint.elements]
for fk_constraint in fk_constraints
],
[],
)
if set(cols) != set(table.c):
return None, None, None
return (
fk_constraints[0].elements[0].column.table,
fk_constraints[1].elements[0].column.table,
fk_constraints,
)
示例7: test_relationship_explicit_override_o2m
# 需要导入模块: from sqlalchemy.ext import automap [as 别名]
# 或者: from sqlalchemy.ext.automap import automap_base [as 别名]
def test_relationship_explicit_override_o2m(self):
Base = automap_base(metadata=self.metadata)
prop = relationship("addresses", collection_class=set)
class User(Base):
__tablename__ = "users"
addresses_collection = prop
Base.prepare()
assert User.addresses_collection.property is prop
Address = Base.classes.addresses
a1 = Address(email_address="e1")
u1 = User(name="u1", addresses_collection=set([a1]))
assert a1.user is u1
示例8: test_exception_prepare_not_called
# 需要导入模块: from sqlalchemy.ext import automap [as 别名]
# 或者: from sqlalchemy.ext.automap import automap_base [as 别名]
def test_exception_prepare_not_called(self):
Base = automap_base(metadata=self.metadata)
class User(Base):
__tablename__ = "users"
s = Session()
assert_raises_message(
orm_exc.UnmappedClassError,
"Class test.ext.test_automap.User is a subclass of AutomapBase. "
r"Mappings are not produced until the .prepare\(\) method is "
"called on the class hierarchy.",
s.query,
User,
)
示例9: test_prepare_accepts_optional_schema_arg
# 需要导入模块: from sqlalchemy.ext import automap [as 别名]
# 或者: from sqlalchemy.ext.automap import automap_base [as 别名]
def test_prepare_accepts_optional_schema_arg(self):
"""
The underlying reflect call accepts an optional schema argument.
This is for determining which database schema to load.
This test verifies that prepare can accept an optional schema
argument and pass it to reflect.
"""
Base = automap_base(metadata=self.metadata)
engine_mock = Mock()
with patch.object(Base.metadata, "reflect") as reflect_mock:
Base.prepare(engine_mock, reflect=True, schema="some_schema")
reflect_mock.assert_called_once_with(
engine_mock,
schema="some_schema",
extend_existing=True,
autoload_replace=False,
)
示例10: test_prepare_defaults_to_no_schema
# 需要导入模块: from sqlalchemy.ext import automap [as 别名]
# 或者: from sqlalchemy.ext.automap import automap_base [as 别名]
def test_prepare_defaults_to_no_schema(self):
"""
The underlying reflect call accepts an optional schema argument.
This is for determining which database schema to load.
This test verifies that prepare passes a default None if no schema is
provided.
"""
Base = automap_base(metadata=self.metadata)
engine_mock = Mock()
with patch.object(Base.metadata, "reflect") as reflect_mock:
Base.prepare(engine_mock, reflect=True)
reflect_mock.assert_called_once_with(
engine_mock,
schema=None,
extend_existing=True,
autoload_replace=False,
)
示例11: test_single_inheritance_reflect
# 需要导入模块: from sqlalchemy.ext import automap [as 别名]
# 或者: from sqlalchemy.ext.automap import automap_base [as 别名]
def test_single_inheritance_reflect(self):
Base = automap_base()
class Single(Base):
__tablename__ = "single"
type = Column(String)
__mapper_args__ = {
"polymorphic_identity": "u0",
"polymorphic_on": type,
}
class SubUser1(Single):
__mapper_args__ = {"polymorphic_identity": "u1"}
class SubUser2(Single):
__mapper_args__ = {"polymorphic_identity": "u2"}
Base.prepare(engine=testing.db, reflect=True)
assert SubUser2.__mapper__.inherits is Single.__mapper__
示例12: test_joined_inheritance_reflect
# 需要导入模块: from sqlalchemy.ext import automap [as 别名]
# 或者: from sqlalchemy.ext.automap import automap_base [as 别名]
def test_joined_inheritance_reflect(self):
Base = automap_base()
class Joined(Base):
__tablename__ = "joined_base"
type = Column(String)
__mapper_args__ = {
"polymorphic_identity": "u0",
"polymorphic_on": type,
}
class SubJoined(Joined):
__tablename__ = "joined_inh"
__mapper_args__ = {"polymorphic_identity": "u1"}
Base.prepare(engine=testing.db, reflect=True)
assert SubJoined.__mapper__.inherits is Joined.__mapper__
assert not Joined.__mapper__.relationships
assert not SubJoined.__mapper__.relationships
示例13: _is_many_to_many
# 需要导入模块: from sqlalchemy.ext import automap [as 别名]
# 或者: from sqlalchemy.ext.automap import automap_base [as 别名]
def _is_many_to_many(automap_base, table):
fk_constraints = [const for const in table.constraints
if isinstance(const, ForeignKeyConstraint)]
if len(fk_constraints) != 2:
return None, None, None
cols = sum(
[[fk.parent for fk in fk_constraint.elements]
for fk_constraint in fk_constraints], [])
if set(cols) != set(table.c):
return None, None, None
return (
fk_constraints[0].elements[0].column.table,
fk_constraints[1].elements[0].column.table,
fk_constraints
)
示例14: automap_base
# 需要导入模块: from sqlalchemy.ext import automap [as 别名]
# 或者: from sqlalchemy.ext.automap import automap_base [as 别名]
def automap_base(declarative_base=None, **kw):
"""Produce a declarative automap base.
This function produces a new base class that is a product of the
:class:`.AutomapBase` class as well a declarative base produced by
:func:`.declarative.declarative_base`.
All parameters other than ``declarative_base`` are keyword arguments
that are passed directly to the :func:`.declarative.declarative_base`
function.
:param declarative_base: an existing class produced by
:func:`.declarative.declarative_base`. When this is passed, the function
no longer invokes :func:`.declarative.declarative_base` itself, and all
other keyword arguments are ignored.
:param \**kw: keyword arguments are passed along to
:func:`.declarative.declarative_base`.
"""
if declarative_base is None:
Base = _declarative_base(**kw)
else:
Base = declarative_base
return type(
Base.__name__,
(AutomapBase, Base,),
{"__abstract__": True, "classes": util.Properties({})}
)
示例15: init_db
# 需要导入模块: from sqlalchemy.ext import automap [as 别名]
# 或者: from sqlalchemy.ext.automap import automap_base [as 别名]
def init_db(db_url, mappings):
engine = create_engine(db_url)
metadata = MetaData()
metadata.bind = engine
if mappings:
for name, mapping in mappings.items():
if "table" in mapping and mapping["table"] not in metadata.tables:
create_table(mapping, metadata)
metadata.create_all()
base = automap_base(bind=engine, metadata=metadata)
base.prepare(engine, reflect=True)
session = create_session(bind=engine, autocommit=False)
return session, engine, base