U kf @sTdZddlmZddlZddlmZddlmZddlmZddlmZddlm Z dd lm Z dd lm Z dd lm Z dd lm Z dd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddl m!Z!ddl"m#Z#ddl$m%Z%ddlm&Z&ddl'm(Z(erdd l)m*Z*dd!l+m,Z,dd"l-m.Z.dd#l-m/Z/dd$l0m1Z1dd%l0m2Z2dd&l0m3Z3dd'lm4Z4ed(ed)Z5ed*ed)Z6Gd+d,d,e(Z7d-d.d/d.d0d1d2Z8Gd3d4d4e(Z9d-d-d-d5d.d6d7d8Z:Gd9d:d:e(Z;d-d-d-d5d.d6d;d<Zd>e(Z=e d-d?d@d.d-d-dAdBdCdDdEZ>e d-d?dFd.d-d-dAdGdCdHdEZ>d-d?dId.d-d-dAdJdCdKdEZ>e&edLeefZ?GdMdNdNZ@ejAGdOdPdPZBdddQdAdAdRdSdTZCd-d/dUdVdWdXZDd-dYdZd[d4d:d>d\d]d^d_ZEd-d/d/d`d/dZd[d:d:d>d\da dbdcZFdS)eahDefine an extension to the :mod:`sqlalchemy.ext.declarative` system which automatically generates mapped classes and relationships from a database schema, typically though not necessarily one which is reflected. It is hoped that the :class:`.AutomapBase` system provides a quick and modernized solution to the problem that the very famous `SQLSoup `_ also tries to solve, that of generating a quick and rudimentary object model from an existing database on the fly. By addressing the issue strictly at the mapper configuration level, and integrating fully with existing Declarative class techniques, :class:`.AutomapBase` seeks to provide a well-integrated approach to the issue of expediently auto-generating ad-hoc mappings. .. tip:: The :ref:`automap_toplevel` extension is geared towards a "zero declaration" approach, where a complete ORM model including classes and pre-named relationships can be generated on the fly from a database schema. For applications that still want to use explicit class declarations including explicit relationship definitions in conjunction with reflection of tables, the :class:`.DeferredReflection` class, described at :ref:`orm_declarative_reflected_deferred_reflection`, is a better choice. .. _automap_basic_use: Basic Use ========= The simplest usage is to reflect an existing database into a new model. We create a new :class:`.AutomapBase` class in a similar manner as to how we create a declarative base class, using :func:`.automap_base`. We then call :meth:`.AutomapBase.prepare` on the resulting base class, asking it to reflect the schema and produce mappings:: from sqlalchemy.ext.automap import automap_base from sqlalchemy.orm import Session from sqlalchemy import create_engine Base = automap_base() # engine, suppose it has two tables 'user' and 'address' set up engine = create_engine("sqlite:///mydatabase.db") # reflect the tables Base.prepare(autoload_with=engine) # mapped classes are now created with names by default # matching that of the table name. User = Base.classes.user Address = Base.classes.address session = Session(engine) # rudimentary relationships are produced session.add(Address(email_address="foo@bar.com", user=User(name="foo"))) session.commit() # collection-based relationships are by default named # "_collection" u1 = session.query(User).first() print(u1.address_collection) Above, calling :meth:`.AutomapBase.prepare` while passing along the :paramref:`.AutomapBase.prepare.reflect` parameter indicates that the :meth:`_schema.MetaData.reflect` method will be called on this declarative base classes' :class:`_schema.MetaData` collection; then, each **viable** :class:`_schema.Table` within the :class:`_schema.MetaData` will get a new mapped class generated automatically. The :class:`_schema.ForeignKeyConstraint` objects which link the various tables together will be used to produce new, bidirectional :func:`_orm.relationship` objects between classes. The classes and relationships follow along a default naming scheme that we can customize. At this point, our basic mapping consisting of related ``User`` and ``Address`` classes is ready to use in the traditional way. .. note:: By **viable**, we mean that for a table to be mapped, it must specify a primary key. Additionally, if the table is detected as being a pure association table between two other tables, it will not be directly mapped and will instead be configured as a many-to-many table between the mappings for the two referring tables. Generating Mappings from an Existing MetaData ============================================= We can pass a pre-declared :class:`_schema.MetaData` object to :func:`.automap_base`. This object can be constructed in any way, including programmatically, from a serialized file, or from itself being reflected using :meth:`_schema.MetaData.reflect`. Below we illustrate a combination of reflection and explicit table declaration:: from sqlalchemy import create_engine, MetaData, Table, Column, ForeignKey from sqlalchemy.ext.automap import automap_base engine = create_engine("sqlite:///mydatabase.db") # produce our own MetaData object metadata = MetaData() # we can reflect it ourselves from a database, using options # such as 'only' to limit what tables we look at... metadata.reflect(engine, only=["user", "address"]) # ... or just define our own Table objects with it (or combine both) Table( "user_order", metadata, Column("id", Integer, primary_key=True), Column("user_id", ForeignKey("user.id")), ) # we can then produce a set of mappings from this MetaData. Base = automap_base(metadata=metadata) # calling prepare() just sets up mapped classes and relationships. Base.prepare() # mapped classes are ready User = Base.classes.user Address = Base.classes.address Order = Base.classes.user_order .. _automap_by_module: Generating Mappings from Multiple Schemas ========================================= The :meth:`.AutomapBase.prepare` method when used with reflection may reflect tables from one schema at a time at most, using the :paramref:`.AutomapBase.prepare.schema` parameter to indicate the name of a schema to be reflected from. In order to populate the :class:`.AutomapBase` with tables from multiple schemas, :meth:`.AutomapBase.prepare` may be invoked multiple times, each time passing a different name to the :paramref:`.AutomapBase.prepare.schema` parameter. The :meth:`.AutomapBase.prepare` method keeps an internal list of :class:`_schema.Table` objects that have already been mapped, and will add new mappings only for those :class:`_schema.Table` objects that are new since the last time :meth:`.AutomapBase.prepare` was run:: e = create_engine("postgresql://scott:tiger@localhost/test") Base.metadata.create_all(e) Base = automap_base() Base.prepare(e) Base.prepare(e, schema="test_schema") Base.prepare(e, schema="test_schema_2") .. versionadded:: 2.0 The :meth:`.AutomapBase.prepare` method may be called any number of times; only newly added tables will be mapped on each run. Previously in version 1.4 and earlier, multiple calls would cause errors as it would attempt to re-map an already mapped class. The previous workaround approach of invoking :meth:`_schema.MetaData.reflect` directly remains available as well. Automapping same-named tables across multiple schemas ----------------------------------------------------- For the common case where multiple schemas may have same-named tables and therefore would generate same-named classes, conflicts can be resolved either through use of the :paramref:`.AutomapBase.prepare.classname_for_table` hook to apply different classnames on a per-schema basis, or by using the :paramref:`.AutomapBase.prepare.modulename_for_table` hook, which allows disambiguation of same-named classes by changing their effective ``__module__`` attribute. In the example below, this hook is used to create a ``__module__`` attribute for all classes that is of the form ``mymodule.``, where the schema name ``default`` is used if no schema is present:: e = create_engine("postgresql://scott:tiger@localhost/test") Base.metadata.create_all(e) def module_name_for_table(cls, tablename, table): if table.schema is not None: return f"mymodule.{table.schema}" else: return f"mymodule.default" Base = automap_base() Base.prepare(e, modulename_for_table=module_name_for_table) Base.prepare(e, schema="test_schema", modulename_for_table=module_name_for_table) Base.prepare(e, schema="test_schema_2", modulename_for_table=module_name_for_table) The same named-classes are organized into a hierarchical collection available at :attr:`.AutomapBase.by_module`. This collection is traversed using the dot-separated name of a particular package/module down into the desired class name. .. note:: When using the :paramref:`.AutomapBase.prepare.modulename_for_table` hook to return a new ``__module__`` that is not ``None``, the class is **not** placed into the :attr:`.AutomapBase.classes` collection; only classes that were not given an explicit modulename are placed here, as the collection cannot represent same-named classes individually. In the example above, if the database contained a table named ``accounts`` in all three of the default schema, the ``test_schema`` schema, and the ``test_schema_2`` schema, three separate classes will be available as:: Base.by_module.mymodule.default.accounts Base.by_module.mymodule.test_schema.accounts Base.by_module.mymodule.test_schema_2.accounts The default module namespace generated for all :class:`.AutomapBase` classes is ``sqlalchemy.ext.automap``. If no :paramref:`.AutomapBase.prepare.modulename_for_table` hook is used, the contents of :attr:`.AutomapBase.by_module` will be entirely within the ``sqlalchemy.ext.automap`` namespace (e.g. ``MyBase.by_module.sqlalchemy.ext.automap.``), which would contain the same series of classes as what would be seen in :attr:`.AutomapBase.classes`. Therefore it's generally only necessary to use :attr:`.AutomapBase.by_module` when explicit ``__module__`` conventions are present. .. versionadded: 2.0 Added the :attr:`.AutomapBase.by_module` collection, which stores classes within a named hierarchy based on dot-separated module names, as well as the :paramref:`.Automap.prepare.modulename_for_table` parameter which allows for custom ``__module__`` schemes for automapped classes. Specifying Classes Explicitly ============================= .. tip:: If explicit classes are expected to be prominent in an application, consider using :class:`.DeferredReflection` instead. The :mod:`.sqlalchemy.ext.automap` extension allows classes to be defined explicitly, in a way similar to that of the :class:`.DeferredReflection` class. Classes that extend from :class:`.AutomapBase` act like regular declarative classes, but are not immediately mapped after their construction, and are instead mapped when we call :meth:`.AutomapBase.prepare`. The :meth:`.AutomapBase.prepare` method will make use of the classes we've established based on the table name we use. If our schema contains tables ``user`` and ``address``, we can define one or both of the classes to be used:: from sqlalchemy.ext.automap import automap_base from sqlalchemy import create_engine # automap base Base = automap_base() # pre-declare User for the 'user' table class User(Base): __tablename__ = "user" # override schema elements like Columns user_name = Column("name", String) # override relationships too, if desired. # we must use the same name that automap would use for the # relationship, and also must refer to the class name that automap will # generate for "address" address_collection = relationship("address", collection_class=set) # reflect engine = create_engine("sqlite:///mydatabase.db") Base.prepare(autoload_with=engine) # we still have Address generated from the tablename "address", # but User is the same as Base.classes.User now Address = Base.classes.address u1 = session.query(User).first() print(u1.address_collection) # the backref is still there: a1 = session.query(Address).first() print(a1.user) Above, one of the more intricate details is that we illustrated overriding one of the :func:`_orm.relationship` objects that automap would have created. To do this, we needed to make sure the names match up with what automap would normally generate, in that the relationship name would be ``User.address_collection`` and the name of the class referred to, from automap's perspective, is called ``address``, even though we are referring to it as ``Address`` within our usage of this class. Overriding Naming Schemes ========================= :mod:`.sqlalchemy.ext.automap` is tasked with producing mapped classes and relationship names based on a schema, which means it has decision points in how these names are determined. These three decision points are provided using functions which can be passed to the :meth:`.AutomapBase.prepare` method, and are known as :func:`.classname_for_table`, :func:`.name_for_scalar_relationship`, and :func:`.name_for_collection_relationship`. Any or all of these functions are provided as in the example below, where we use a "camel case" scheme for class names and a "pluralizer" for collection names using the `Inflect `_ package:: import re import inflect def camelize_classname(base, tablename, table): "Produce a 'camelized' class name, e.g." "'words_and_underscores' -> 'WordsAndUnderscores'" return str( tablename[0].upper() + re.sub( r"_([a-z])", lambda m: m.group(1).upper(), tablename[1:], ) ) _pluralizer = inflect.engine() def pluralize_collection(base, local_cls, referred_cls, constraint): "Produce an 'uncamelized', 'pluralized' class name, e.g." "'SomeTerm' -> 'some_terms'" referred_name = referred_cls.__name__ uncamelized = re.sub( r"[A-Z]", lambda m: "_%s" % m.group(0).lower(), referred_name, )[1:] pluralized = _pluralizer.plural(uncamelized) return pluralized from sqlalchemy.ext.automap import automap_base Base = automap_base() engine = create_engine("sqlite:///mydatabase.db") Base.prepare( autoload_with=engine, classname_for_table=camelize_classname, name_for_collection_relationship=pluralize_collection, ) From the above mapping, we would now have classes ``User`` and ``Address``, where the collection from ``User`` to ``Address`` is called ``User.addresses``:: User, Address = Base.classes.User, Base.classes.Address u1 = User(addresses=[Address(email="foo@bar.com")]) Relationship Detection ====================== The vast majority of what automap accomplishes is the generation of :func:`_orm.relationship` structures based on foreign keys. The mechanism by which this works for many-to-one and one-to-many relationships is as follows: 1. A given :class:`_schema.Table`, known to be mapped to a particular class, is examined for :class:`_schema.ForeignKeyConstraint` objects. 2. From each :class:`_schema.ForeignKeyConstraint`, the remote :class:`_schema.Table` object present is matched up to the class to which it is to be mapped, if any, else it is skipped. 3. As the :class:`_schema.ForeignKeyConstraint` we are examining corresponds to a reference from the immediate mapped class, the relationship will be set up as a many-to-one referring to the referred class; a corresponding one-to-many backref will be created on the referred class referring to this class. 4. If any of the columns that are part of the :class:`_schema.ForeignKeyConstraint` are not nullable (e.g. ``nullable=False``), a :paramref:`_orm.relationship.cascade` keyword argument of ``all, delete-orphan`` will be added to the keyword arguments to be passed to the relationship or backref. If the :class:`_schema.ForeignKeyConstraint` reports that :paramref:`_schema.ForeignKeyConstraint.ondelete` is set to ``CASCADE`` for a not null or ``SET NULL`` for a nullable set of columns, the option :paramref:`_orm.relationship.passive_deletes` flag is set to ``True`` in the set of relationship keyword arguments. Note that not all backends support reflection of ON DELETE. 5. The names of the relationships are determined using the :paramref:`.AutomapBase.prepare.name_for_scalar_relationship` and :paramref:`.AutomapBase.prepare.name_for_collection_relationship` callable functions. It is important to note that the default relationship naming derives the name from the **the actual class name**. If you've given a particular class an explicit name by declaring it, or specified an alternate class naming scheme, that's the name from which the relationship name will be derived. 6. The classes are inspected for an existing mapped property matching these names. If one is detected on one side, but none on the other side, :class:`.AutomapBase` attempts to create a relationship on the missing side, then uses the :paramref:`_orm.relationship.back_populates` parameter in order to point the new relationship to the other side. 7. In the usual case where no relationship is on either side, :meth:`.AutomapBase.prepare` produces a :func:`_orm.relationship` on the "many-to-one" side and matches it to the other using the :paramref:`_orm.relationship.backref` parameter. 8. Production of the :func:`_orm.relationship` and optionally the :func:`.backref` is handed off to the :paramref:`.AutomapBase.prepare.generate_relationship` function, which can be supplied by the end-user in order to augment the arguments passed to :func:`_orm.relationship` or :func:`.backref` or to make use of custom implementations of these functions. Custom Relationship Arguments ----------------------------- The :paramref:`.AutomapBase.prepare.generate_relationship` hook can be used to add parameters to relationships. For most cases, we can make use of the existing :func:`.automap.generate_relationship` function to return the object, after augmenting the given keyword dictionary with our own arguments. Below is an illustration of how to send :paramref:`_orm.relationship.cascade` and :paramref:`_orm.relationship.passive_deletes` options along to all one-to-many relationships:: from sqlalchemy.ext.automap import generate_relationship from sqlalchemy.orm import interfaces def _gen_relationship( base, direction, return_fn, attrname, local_cls, referred_cls, **kw ): if direction is interfaces.ONETOMANY: kw["cascade"] = "all, delete-orphan" kw["passive_deletes"] = True # make use of the built-in function to actually return # the result. return generate_relationship( base, direction, return_fn, attrname, local_cls, referred_cls, **kw ) from sqlalchemy.ext.automap import automap_base from sqlalchemy import create_engine # automap base Base = automap_base() engine = create_engine("sqlite:///mydatabase.db") Base.prepare(autoload_with=engine, generate_relationship=_gen_relationship) Many-to-Many relationships -------------------------- :mod:`.sqlalchemy.ext.automap` will generate many-to-many relationships, e.g. those which contain a ``secondary`` argument. The process for producing these is as follows: 1. A given :class:`_schema.Table` is examined for :class:`_schema.ForeignKeyConstraint` objects, before any mapped class has been assigned to it. 2. If the table contains two and exactly two :class:`_schema.ForeignKeyConstraint` objects, and all columns within this table are members of these two :class:`_schema.ForeignKeyConstraint` objects, the table is assumed to be a "secondary" table, and will **not be mapped directly**. 3. The two (or one, for self-referential) external tables to which the :class:`_schema.Table` refers to are matched to the classes to which they will be mapped, if any. 4. If mapped classes for both sides are located, a many-to-many bi-directional :func:`_orm.relationship` / :func:`.backref` pair is created between the two classes. 5. The override logic for many-to-many works the same as that of one-to-many/ many-to-one; the :func:`.generate_relationship` function is called upon to generate the structures and existing attributes will be maintained. Relationships with Inheritance ------------------------------ :mod:`.sqlalchemy.ext.automap` will not generate any relationships between two classes that are in an inheritance relationship. That is, with two classes given as follows:: class Employee(Base): __tablename__ = "employee" id = Column(Integer, primary_key=True) type = Column(String(50)) __mapper_args__ = { "polymorphic_identity": "employee", "polymorphic_on": type, } class Engineer(Employee): __tablename__ = "engineer" id = Column(Integer, ForeignKey("employee.id"), primary_key=True) __mapper_args__ = { "polymorphic_identity": "engineer", } The foreign key from ``Engineer`` to ``Employee`` is used not for a relationship, but to establish joined inheritance between the two classes. Note that this means automap will not generate *any* relationships for foreign keys that link from a subclass to a superclass. If a mapping has actual relationships from subclass to superclass as well, those need to be explicit. Below, as we have two separate foreign keys from ``Engineer`` to ``Employee``, we need to set up both the relationship we want as well as the ``inherit_condition``, as these are not things SQLAlchemy can guess:: class Employee(Base): __tablename__ = "employee" id = Column(Integer, primary_key=True) type = Column(String(50)) __mapper_args__ = { "polymorphic_identity": "employee", "polymorphic_on": type, } class Engineer(Employee): __tablename__ = "engineer" id = Column(Integer, ForeignKey("employee.id"), primary_key=True) favorite_employee_id = Column(Integer, ForeignKey("employee.id")) favorite_employee = relationship(Employee, foreign_keys=favorite_employee_id) __mapper_args__ = { "polymorphic_identity": "engineer", "inherit_condition": id == Employee.id, } Handling Simple Naming Conflicts -------------------------------- In the case of naming conflicts during mapping, override any of :func:`.classname_for_table`, :func:`.name_for_scalar_relationship`, and :func:`.name_for_collection_relationship` as needed. For example, if automap is attempting to name a many-to-one relationship the same as an existing column, an alternate convention can be conditionally selected. Given a schema: .. sourcecode:: sql CREATE TABLE table_a ( id INTEGER PRIMARY KEY ); CREATE TABLE table_b ( id INTEGER PRIMARY KEY, table_a INTEGER, FOREIGN KEY(table_a) REFERENCES table_a(id) ); The above schema will first automap the ``table_a`` table as a class named ``table_a``; it will then automap a relationship onto the class for ``table_b`` with the same name as this related class, e.g. ``table_a``. This relationship name conflicts with the mapping column ``table_b.table_a``, and will emit an error on mapping. We can resolve this conflict by using an underscore as follows:: def name_for_scalar_relationship(base, local_cls, referred_cls, constraint): name = referred_cls.__name__.lower() local_table = local_cls.__table__ if name in local_table.columns: newname = name + "_" warnings.warn("Already detected name %s present. using %s" % (name, newname)) return newname return name Base.prepare( autoload_with=engine, name_for_scalar_relationship=name_for_scalar_relationship, ) Alternatively, we can change the name on the column side. The columns that are mapped can be modified using the technique described at :ref:`mapper_column_distinct_names`, by assigning the column explicitly to a new name:: Base = automap_base() class TableB(Base): __tablename__ = "table_b" _table_a = Column("table_a", ForeignKey("table_a.id")) Base.prepare(autoload_with=engine) Using Automap with Explicit Declarations ======================================== As noted previously, automap has no dependency on reflection, and can make use of any collection of :class:`_schema.Table` objects within a :class:`_schema.MetaData` collection. From this, it follows that automap can also be used generate missing relationships given an otherwise complete model that fully defines table metadata:: from sqlalchemy.ext.automap import automap_base from sqlalchemy import Column, Integer, String, ForeignKey Base = automap_base() class User(Base): __tablename__ = "user" id = Column(Integer, primary_key=True) name = Column(String) class Address(Base): __tablename__ = "address" id = Column(Integer, primary_key=True) email = Column(String) user_id = Column(ForeignKey("user.id")) # produce relationships Base.prepare() # mapping is complete, with "address_collection" and # "user" relationships a1 = Address(email="u1") a2 = Address(email="u2") u1 = User(address_collection=[a1, a2]) assert a1.user is u1 Above, given mostly complete ``User`` and ``Address`` mappings, the :class:`_schema.ForeignKey` which we defined on ``Address.user_id`` allowed a bidirectional relationship pair ``Address.user`` and ``User.address_collection`` to be generated on the mapped classes. Note that when subclassing :class:`.AutomapBase`, the :meth:`.AutomapBase.prepare` method is required; if not called, the classes we've declared are in an un-mapped state. .. _automap_intercepting_columns: Intercepting Column Definitions =============================== The :class:`_schema.MetaData` and :class:`_schema.Table` objects support an event hook :meth:`_events.DDLEvents.column_reflect` that may be used to intercept the information reflected about a database column before the :class:`_schema.Column` object is constructed. For example if we wanted to map columns using a naming convention such as ``"attr_"``, the event could be applied as:: @event.listens_for(Base.metadata, "column_reflect") def column_reflect(inspector, table, column_info): # set column.key = "attr_" column_info["key"] = "attr_%s" % column_info["name"].lower() # run reflection Base.prepare(autoload_with=engine) .. versionadded:: 1.4.0b2 the :meth:`_events.DDLEvents.column_reflect` event may be applied to a :class:`_schema.MetaData` object. .. seealso:: :meth:`_events.DDLEvents.column_reflect` :ref:`mapper_automated_reflection_schemes` - in the ORM mapping documentation ) annotationsN)Any)Callable)cast)ClassVar)Dict)List)NoReturn)Optional)overload)Set)Tuple)Type) TYPE_CHECKING)TypeVar)Union)util)backref)declarative_base)exc) interfaces) relationship)_DeferredMapperConfig)_CONFIGURE_MUTEX)ForeignKeyConstraint)and_) Properties)Protocol)Engine)RelationshipDirection)ORMBackrefArgument) Relationship)Column)MetaDataTable) immutabledict_KT)bound_VTc@s eZdZdddddddZdS)PythonNameForTableType Type[Any]strr&base tablenametablereturncCsdSN)selfr/r0r1r4r4F/opt/hc_python/lib64/python3.8/site-packages/sqlalchemy/ext/automap.py__call__szPythonNameForTableType.__call__N__name__ __module__ __qualname__r7r4r4r4r6r+sr+r,r-r&r.cCst|S)alReturn the class name that should be used, given the name of a table. The default implementation is:: return str(tablename) Alternate implementations can be specified using the :paramref:`.AutomapBase.prepare.classname_for_table` parameter. :param base: the :class:`.AutomapBase` class doing the prepare. :param tablename: string name of the :class:`_schema.Table`. :param table: the :class:`_schema.Table` object itself. :return: a string class name. .. note:: In Python 2, the string used for the class name **must** be a non-Unicode object, e.g. a ``str()`` object. The ``.name`` attribute of :class:`_schema.Table` is typically a Python unicode subclass, so the ``str()`` function should be applied to this name, after accounting for any non-ASCII characters. )r-)r/r0r1r4r4r6classname_for_tables"r<c@s"eZdZddddddddZdS)NameForScalarRelationshipTyper,rr-r/ local_cls referred_cls constraintr2cCsdSr3r4r5r/r?r@rAr4r4r6r7sz&NameForScalarRelationshipType.__call__Nr8r4r4r4r6r=sr=rr>cCs |jS)aReturn the attribute name that should be used to refer from one class to another, for a scalar object reference. The default implementation is:: return referred_cls.__name__.lower() Alternate implementations can be specified using the :paramref:`.AutomapBase.prepare.name_for_scalar_relationship` parameter. :param base: the :class:`.AutomapBase` class doing the prepare. :param local_cls: the class to be mapped on the local side. :param referred_cls: the class to be mapped on the referring side. :param constraint: the :class:`_schema.ForeignKeyConstraint` that is being inspected to produce this relationship. r9lowerr/r?r@rAr4r4r6name_for_scalar_relationship#srFc@s"eZdZddddddddZdS)!NameForCollectionRelationshipTyper,rr-r>cCsdSr3r4rBr4r4r6r7Bsz*NameForCollectionRelationshipType.__call__Nr8r4r4r4r6rGAsrGcCs|jdS)aReturn the attribute name that should be used to refer from one class to another, for a collection reference. The default implementation is:: return referred_cls.__name__.lower() + "_collection" Alternate implementations can be specified using the :paramref:`.AutomapBase.prepare.name_for_collection_relationship` parameter. :param base: the :class:`.AutomapBase` class doing the prepare. :param local_cls: the class to be mapped on the local side. :param referred_cls: the class to be mapped on the referring side. :param constraint: the :class:`_schema.ForeignKeyConstraint` that is being inspected to produce this relationship. Z _collectionrCrEr4r4r6 name_for_collection_relationshipKsrHc @sheZdZeddddddddddd Zeddd ddddd dd d Zddd dddddddd ZdS)GenerateRelationshipTyper,r Callable[..., Relationship[Any]]r-rRelationship[Any]r/ direction return_fnattrnamer?r@kwr2cKsdSr3r4r5r/rMrNrOr?r@rPr4r4r6r7ks z!GenerateRelationshipType.__call__!Callable[..., ORMBackrefArgument]r!cKsdSr3r4rQr4r4r6r7ws JUnion[Callable[..., Relationship[Any]], Callable[..., ORMBackrefArgument]]z,Union[ORMBackrefArgument, Relationship[Any]]cKsdSr3r4rQr4r4r6r7s N)r9r:r;r r7r4r4r4r6rIjs   rIr rJrrKrLcKsdSr3r4r/rMrNrOr?r@rPr4r4r6generate_relationships rUrRr!cKsdSr3r4rTr4r4r6rUs rSz,Union[Relationship[Any], ORMBackrefArgument]cKs8|tkr||f|S|tkr(||f|Std|dS)aGenerate a :func:`_orm.relationship` or :func:`.backref` on behalf of two mapped classes. An alternate implementation of this function can be specified using the :paramref:`.AutomapBase.prepare.generate_relationship` parameter. The default implementation of this function is as follows:: if return_fn is backref: return return_fn(attrname, **kw) elif return_fn is relationship: return return_fn(referred_cls, **kw) else: raise TypeError("Unknown relationship function: %s" % return_fn) :param base: the :class:`.AutomapBase` class doing the prepare. :param direction: indicate the "direction" of the relationship; this will be one of :data:`.ONETOMANY`, :data:`.MANYTOONE`, :data:`.MANYTOMANY`. :param return_fn: the function that is used by default to create the relationship. This will be either :func:`_orm.relationship` or :func:`.backref`. The :func:`.backref` function's result will be used to produce a new :func:`_orm.relationship` in a second step, so it is critical that user-defined implementations correctly differentiate between the two functions, if a custom relationship function is being used. :param attrname: the attribute name to which this relationship is being assigned. If the value of :paramref:`.generate_relationship.return_fn` is the :func:`.backref` function, then this name is the name that is being assigned to the backref. :param local_cls: the "local" class to which this relationship or backref will be locally present. :param referred_cls: the "referred" class to which the relationship or backref refers to. :param \**kw: all additional keyword arguments are passed along to the function. :return: a :func:`_orm.relationship` or :func:`.backref` construct, as dictated by the :paramref:`.generate_relationship.return_fn` parameter. z!Unknown relationship function: %sN)rr TypeErrorrTr4r4r6rUs <  ByModulePropertiesc@seZdZUdZdZded<ded<ded<d ed <eejd d d ddddddddddej f dddddddddddddd ddZ dZ eddd d!Z dS)" AutomapBaseaBase class for an "automap" schema. The :class:`.AutomapBase` class can be compared to the "declarative base" class that is produced by the :func:`.declarative.declarative_base` function. In practice, the :class:`.AutomapBase` class is always used as a mixin along with an actual declarative base. A new subclassable :class:`.AutomapBase` is typically instantiated using the :func:`.automap_base` function. .. seealso:: :ref:`automap_toplevel` TzClassVar[Properties[Type[Any]]]classeszClassVar[ByModuleProperties] by_modulezClassVar[MetaData]metadatazClassVar[_Bookkeeping]_sa_automapbase_bookkeeping)2.0zThe :paramref:`_automap.AutomapBase.prepare.engine` parameter is deprecated and will be removed in a future release. Please use the :paramref:`_automap.AutomapBase.prepare.autoload_with` parameter.)r]zThe :paramref:`_automap.AutomapBase.prepare.reflect` parameter is deprecated and will be removed in a future release. Reflection is enabled when :paramref:`_automap.AutomapBase.prepare.autoload_with` is passed.)enginereflectNFType[AutomapBase]zOptional[Engine]z Optional[Any]boolz Optional[str]z Optional[PythonNameForTableType]z'Optional[NameForScalarRelationshipType]z+Optional[NameForCollectionRelationshipType]z"Optional[GenerateRelationshipType]z.Union[Dict[_KT, _VT], immutabledict[_KT, _VT]]None) cls autoload_withr^r_schemar<modulename_for_tablecollection_classrFrHrUreflection_optionsr2c ! Cs&|jD]} d| jkrtd| } q0qds0tdt}|dkrF|d}|dkrV|d}| dkrf|d} | dkrv|d } |dkrt}|rd }|r|}|r|stt|d dd }| r|| |jj |f|t Ld d t j |ddD}g}| j }|jj}t||jD]}||}|j|t||\}}}|dk rr|dk sNt|dk s\t|||||fq |jsq q ||kr d|i}|dk r|||j|}|dk r||d<nd}|||j|}|dkr||jkrtd|d|jdq t|| f|}t |}|jj|ks0t|dkrD||j|<|j}|jj !dD]:}||krtt"i||<||} t#| t"st| }qX|||jj<|||<q |$D]}t%| ||||| | q|D](\}}}}t&| |||||||| | qt | D]}|'qW5QRXdS)aExtract mapped classes and relationships from the :class:`_schema.MetaData` and perform mappings. For full documentation and examples see :ref:`automap_basic_use`. :param autoload_with: an :class:`_engine.Engine` or :class:`_engine.Connection` with which to perform schema reflection; when specified, the :meth:`_schema.MetaData.reflect` method will be invoked within the scope of this method. :param engine: legacy; use :paramref:`.AutomapBase.autoload_with`. Used to indicate the :class:`_engine.Engine` or :class:`_engine.Connection` with which to reflect tables with, if :paramref:`.AutomapBase.reflect` is True. :param reflect: legacy; use :paramref:`.AutomapBase.autoload_with`. Indicates that :meth:`_schema.MetaData.reflect` should be invoked. :param classname_for_table: callable function which will be used to produce new class names, given a table name. Defaults to :func:`.classname_for_table`. :param modulename_for_table: callable function which will be used to produce the effective ``__module__`` for an internally generated class, to allow for multiple classes of the same name in a single automap base which would be in different "modules". Defaults to ``None``, which will indicate that ``__module__`` will not be set explicitly; the Python runtime will use the value ``sqlalchemy.ext.automap`` for these classes. When assigning ``__module__`` to generated classes, they can be accessed based on dot-separated module names using the :attr:`.AutomapBase.by_module` collection. Classes that have an explicit ``__module_`` assigned using this hook do **not** get placed into the :attr:`.AutomapBase.classes` collection, only into :attr:`.AutomapBase.by_module`. .. versionadded:: 2.0 .. seealso:: :ref:`automap_by_module` :param name_for_scalar_relationship: callable function which will be used to produce relationship names for scalar relationships. Defaults to :func:`.name_for_scalar_relationship`. :param name_for_collection_relationship: callable function which will be used to produce relationship names for collection-oriented relationships. Defaults to :func:`.name_for_collection_relationship`. :param generate_relationship: callable function which will be used to actually generate :func:`_orm.relationship` and :func:`.backref` constructs. Defaults to :func:`.generate_relationship`. :param collection_class: the Python collection class that will be used when a new :func:`_orm.relationship` object is created that represents a collection. Defaults to ``list``. :param schema: Schema name to reflect when reflecting tables using the :paramref:`.AutomapBase.prepare.autoload_with` parameter. The name is passed to the :paramref:`_schema.MetaData.reflect.schema` parameter of :meth:`_schema.MetaData.reflect`. When omitted, the default schema in use by the database connection is used. .. note:: The :paramref:`.AutomapBase.prepare.schema` parameter supports reflection of a single schema at a time. In order to include tables from many schemas, use multiple calls to :meth:`.AutomapBase.prepare`. For an overview of multiple-schema automap including the use of additional naming conventions to resolve table name conflicts, see the section :ref:`automap_by_module`. .. versionadded:: 2.0 :meth:`.AutomapBase.prepare` supports being directly invoked any number of times, keeping track of tables that have already been processed to avoid processing them a second time. :param reflection_options: When present, this dictionary of options will be passed to :meth:`_schema.MetaData.reflect` to supply general reflection-specific options like ``only`` and/or dialect-specific options like ``oracle_resolve_synonyms``. .. versionadded:: 1.4 r\r`Fz,Can't locate automap base in class hierarchyNr<rFrHrUT)reZextend_existingZautoload_replacecSsi|]}td|j|qSr%)r local_table).0mr4r4r6 s z'AutomapBase.prepare..)sortZ __table__r:zIgnoring duplicate class name 'z%' received in automap base for table zS without ``__module__`` being set; consider using the ``modulename_for_table`` hook.)(__mro____dict__rAssertionErrorglobalslistdictupdater[r_rrZclasses_for_baser\Ztablesset difference table_keysadd_is_many_to_manyappendZ primary_keynamerYrwarnkeytypeZconfig_for_clsrcr9rZr:splitr isinstancevalues_relationships_for_fks_m2m_relationshipmap)!rcrdr^r_rer<rfrgrFrHrUrhmr automap_baseZglblsoptstable_to_map_configZ many_to_manyZ bookkeepingZmetadata_tablesZ table_keyr1lcl_m2mrem_m2m m2m_constZclsdictZ new_moduleZnewnameZ mapped_cls map_configZby_module_propertiestokenpropsr4r4r6prepare>s                     zAutomapBase.preparer )r2cCstj|dt|ddS)NzClass %s is a subclass of AutomapBase. Mappings are not produced until the .prepare() method is called on the class hierarchy.)msg)orm_excZUnmappedClassErrorZ_safe_cls_name)rcr4r4r6_sa_raise_deferred_configqs z%AutomapBase._sa_raise_deferred_config) r9r:r;__doc__ __abstract____annotations__ classmethodrZdeprecated_params EMPTY_DICTrZ_sa_decl_preparerr4r4r4r6rXs8  , rXc@seZdZUdZded<dS) _Bookkeeping)rxzSet[str]rxN)r9r:r; __slots__rr4r4r4r6r|s rOptional[Type[Any]])rrPr2cKsF|dkrtf|}n|}t|jt|fdtitittdS)aProduce 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`. NT)rrYrZr\)_declarative_baserr9rXrrrrv)rrPZBaser4r4r6rs rzMTuple[Optional[Table], Optional[Table], Optional[list[ForeignKeyConstraint]]])rr1r2cCspdd|jD}t|dkr dStdd|Dg}t|t|jkrJdS|djdjj|djdjj|fS)NcSsg|]}t|tr|qSr4)rr)rjconstr4r4r6 s z$_is_many_to_many..r)NNNcSsg|]}dd|jDqS)cSsg|] }|jqSr4parentrjZfkr4r4r6rsz/_is_many_to_many...)elements)rjZ fk_constraintr4r4r6rsr) constraintslensumrvcrcolumnr1)rr1Zfk_constraintscolsr4r4r6rzs" rzrzWUnion[Dict[Optional[Table], _DeferredMapperConfig], Dict[Table, _DeferredMapperConfig]]rrb)rrrrgrFrHrUr2c Cstd|j}td|j}|dks(|dkr,dS|jD]} t| tr2| j} | djj} | | d} | dkrjq2| j} || k rt || rq2|||| | }||| || }i}ddd| Dk}|sd|d<| j r| j dkrd |d <n| j r| j d krd |d <|| j k}||j kr|r:||tjt|| |fd |i|}nd}||tjt||| d d| jD|dd| jDd }|dk r||j |<|s|| j |_q2|r2||tjt|| |fdd| jD||d|}|dk r2|| j |<||j |_q2dS)NzOptional[Table]rrFcSsh|] }|jjqSr4)rnullablerr4r4r6 sz)_relationships_for_fks..zall, delete-orphanZcascadeTZpassive_deleteszset nullrgcSsg|] }|jqSr4rrr4r4r6rsz*_relationships_for_fks..cSsg|] }|jqSr4)rrr4r4r6rs) foreign_keysrZ remote_sidecSsg|] }|jqSr4rrr4r4r6r+s)rback_populatesrg)rrircrrrrrr1get issubclassZondeleterD propertiesrZ ONETOMANYrZ MANYTOONErr)rrrrgrFrHrUrir?rAZfksZreferred_table referred_cfgr@relationship_name backref_nameZo2m_kwsrcreate_backref backref_objrelr4r4r6rs                 rzList[ForeignKeyConstraint]) rrrrr1rrgrFrHrUr2c Cs||d} ||d} | dks(| dkr,dS| j} | j} ||| | |d}||| | |d}|| jk}||krtd}nd}|| jkr|r| |tjt|| | ||d}nd}| |tjt|| | ||tdd|djDtdd|djD||d }|dk r|| j|<|s|| j|_ np|r| |tjt|| | ||td d|djDtd d|djD||d }|dk r|| j|<|| j|_ dS) Nrrz__*)rgoverlapscss|]}|j|jkVqdSr3rrrr4r4r6 tsz$_m2m_relationship..css|]}|j|jkVqdSr3rrr4r4r6rws)r secondary primaryjoin secondaryjoinrrgcss|]}|j|jkVqdSr3rrr4r4r6rscss|]}|j|jkVqdSr3rrr4r4r6rs)rrrrrrg) rrcrrZ MANYTOMANYrrrrr)rrrrr1rrgrFrHrUrrr?r@rrrrrrr4r4r6r7s         r)N)Gr __future__r dataclassestypingrrrrrrr r r r r rrrrrZormrrrrrrrZ orm.decl_baserZ orm.mapperrrersqlrrZ util.typingrZ engine.baserZorm.baser Zorm.relationshipsr!r"Z sql.schemar#r$r&r'r(r*r+r<r=rFrGrHrIrUrWrX dataclassrrrzrrr4r4r4r6s<                                      %  '  D'n