當前位置: 首頁>>代碼示例>>Python>>正文


Python automap.automap_base方法代碼示例

本文整理匯總了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
    ) 
開發者ID:jpush,項目名稱:jbox,代碼行數:20,代碼來源:automap.py

示例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 
開發者ID:tmthyjames,項目名稱:SQLCell,代碼行數:25,代碼來源:db.py

示例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) 
開發者ID:SFDO-Tooling,項目名稱:CumulusCI,代碼行數:22,代碼來源:extract.py

示例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
    ) 
開發者ID:gltn,項目名稱:stdm,代碼行數:21,代碼來源:automap.py

示例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,
    ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:27,代碼來源:automap.py

示例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 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:18,代碼來源:test_automap.py

示例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,
        ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:18,代碼來源:test_automap.py

示例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,
            ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:19,代碼來源:test_automap.py

示例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,
            ) 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:19,代碼來源:test_automap.py

示例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__ 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:24,代碼來源:test_automap.py

示例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 
開發者ID:sqlalchemy,項目名稱:sqlalchemy,代碼行數:25,代碼來源:test_automap.py

示例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
    ) 
開發者ID:binhex,項目名稱:moviegrabber,代碼行數:20,代碼來源:automap.py

示例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({})}
    ) 
開發者ID:jpush,項目名稱:jbox,代碼行數:32,代碼來源:automap.py

示例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 
開發者ID:SFDO-Tooling,項目名稱:CumulusCI,代碼行數:15,代碼來源:base_generate_data_task.py


注:本文中的sqlalchemy.ext.automap.automap_base方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。