automap.py 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. # ext/automap.py
  2. # Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. r"""Define an extension to the :mod:`sqlalchemy.ext.declarative` system
  8. which automatically generates mapped classes and relationships from a database
  9. schema, typically though not necessarily one which is reflected.
  10. .. versionadded:: 0.9.1 Added :mod:`sqlalchemy.ext.automap`.
  11. It is hoped that the :class:`.AutomapBase` system provides a quick
  12. and modernized solution to the problem that the very famous
  13. `SQLSoup <https://sqlsoup.readthedocs.io/en/latest/>`_
  14. also tries to solve, that of generating a quick and rudimentary object
  15. model from an existing database on the fly. By addressing the issue strictly
  16. at the mapper configuration level, and integrating fully with existing
  17. Declarative class techniques, :class:`.AutomapBase` seeks to provide
  18. a well-integrated approach to the issue of expediently auto-generating ad-hoc
  19. mappings.
  20. Basic Use
  21. =========
  22. The simplest usage is to reflect an existing database into a new model.
  23. We create a new :class:`.AutomapBase` class in a similar manner as to how
  24. we create a declarative base class, using :func:`.automap_base`.
  25. We then call :meth:`.AutomapBase.prepare` on the resulting base class,
  26. asking it to reflect the schema and produce mappings::
  27. from sqlalchemy.ext.automap import automap_base
  28. from sqlalchemy.orm import Session
  29. from sqlalchemy import create_engine
  30. Base = automap_base()
  31. # engine, suppose it has two tables 'user' and 'address' set up
  32. engine = create_engine("sqlite:///mydatabase.db")
  33. # reflect the tables
  34. Base.prepare(engine, reflect=True)
  35. # mapped classes are now created with names by default
  36. # matching that of the table name.
  37. User = Base.classes.user
  38. Address = Base.classes.address
  39. session = Session(engine)
  40. # rudimentary relationships are produced
  41. session.add(Address(email_address="foo@bar.com", user=User(name="foo")))
  42. session.commit()
  43. # collection-based relationships are by default named
  44. # "<classname>_collection"
  45. print (u1.address_collection)
  46. Above, calling :meth:`.AutomapBase.prepare` while passing along the
  47. :paramref:`.AutomapBase.prepare.reflect` parameter indicates that the
  48. :meth:`.MetaData.reflect` method will be called on this declarative base
  49. classes' :class:`.MetaData` collection; then, each **viable**
  50. :class:`.Table` within the :class:`.MetaData` will get a new mapped class
  51. generated automatically. The :class:`.ForeignKeyConstraint` objects which
  52. link the various tables together will be used to produce new, bidirectional
  53. :func:`.relationship` objects between classes. The classes and relationships
  54. follow along a default naming scheme that we can customize. At this point,
  55. our basic mapping consisting of related ``User`` and ``Address`` classes is
  56. ready to use in the traditional way.
  57. .. note:: By **viable**, we mean that for a table to be mapped, it must
  58. specify a primary key. Additionally, if the table is detected as being
  59. a pure association table between two other tables, it will not be directly
  60. mapped and will instead be configured as a many-to-many table between
  61. the mappings for the two referring tables.
  62. Generating Mappings from an Existing MetaData
  63. =============================================
  64. We can pass a pre-declared :class:`.MetaData` object to :func:`.automap_base`.
  65. This object can be constructed in any way, including programmatically, from
  66. a serialized file, or from itself being reflected using
  67. :meth:`.MetaData.reflect`. Below we illustrate a combination of reflection and
  68. explicit table declaration::
  69. from sqlalchemy import create_engine, MetaData, Table, Column, ForeignKey
  70. engine = create_engine("sqlite:///mydatabase.db")
  71. # produce our own MetaData object
  72. metadata = MetaData()
  73. # we can reflect it ourselves from a database, using options
  74. # such as 'only' to limit what tables we look at...
  75. metadata.reflect(engine, only=['user', 'address'])
  76. # ... or just define our own Table objects with it (or combine both)
  77. Table('user_order', metadata,
  78. Column('id', Integer, primary_key=True),
  79. Column('user_id', ForeignKey('user.id'))
  80. )
  81. # we can then produce a set of mappings from this MetaData.
  82. Base = automap_base(metadata=metadata)
  83. # calling prepare() just sets up mapped classes and relationships.
  84. Base.prepare()
  85. # mapped classes are ready
  86. User, Address, Order = Base.classes.user, Base.classes.address,\
  87. Base.classes.user_order
  88. Specifying Classes Explicitly
  89. =============================
  90. The :mod:`.sqlalchemy.ext.automap` extension allows classes to be defined
  91. explicitly, in a way similar to that of the :class:`.DeferredReflection` class.
  92. Classes that extend from :class:`.AutomapBase` act like regular declarative
  93. classes, but are not immediately mapped after their construction, and are
  94. instead mapped when we call :meth:`.AutomapBase.prepare`. The
  95. :meth:`.AutomapBase.prepare` method will make use of the classes we've
  96. established based on the table name we use. If our schema contains tables
  97. ``user`` and ``address``, we can define one or both of the classes to be used::
  98. from sqlalchemy.ext.automap import automap_base
  99. from sqlalchemy import create_engine
  100. # automap base
  101. Base = automap_base()
  102. # pre-declare User for the 'user' table
  103. class User(Base):
  104. __tablename__ = 'user'
  105. # override schema elements like Columns
  106. user_name = Column('name', String)
  107. # override relationships too, if desired.
  108. # we must use the same name that automap would use for the
  109. # relationship, and also must refer to the class name that automap will
  110. # generate for "address"
  111. address_collection = relationship("address", collection_class=set)
  112. # reflect
  113. engine = create_engine("sqlite:///mydatabase.db")
  114. Base.prepare(engine, reflect=True)
  115. # we still have Address generated from the tablename "address",
  116. # but User is the same as Base.classes.User now
  117. Address = Base.classes.address
  118. u1 = session.query(User).first()
  119. print (u1.address_collection)
  120. # the backref is still there:
  121. a1 = session.query(Address).first()
  122. print (a1.user)
  123. Above, one of the more intricate details is that we illustrated overriding
  124. one of the :func:`.relationship` objects that automap would have created.
  125. To do this, we needed to make sure the names match up with what automap
  126. would normally generate, in that the relationship name would be
  127. ``User.address_collection`` and the name of the class referred to, from
  128. automap's perspective, is called ``address``, even though we are referring to
  129. it as ``Address`` within our usage of this class.
  130. Overriding Naming Schemes
  131. =========================
  132. :mod:`.sqlalchemy.ext.automap` is tasked with producing mapped classes and
  133. relationship names based on a schema, which means it has decision points in how
  134. these names are determined. These three decision points are provided using
  135. functions which can be passed to the :meth:`.AutomapBase.prepare` method, and
  136. are known as :func:`.classname_for_table`,
  137. :func:`.name_for_scalar_relationship`,
  138. and :func:`.name_for_collection_relationship`. Any or all of these
  139. functions are provided as in the example below, where we use a "camel case"
  140. scheme for class names and a "pluralizer" for collection names using the
  141. `Inflect <https://pypi.python.org/pypi/inflect>`_ package::
  142. import re
  143. import inflect
  144. def camelize_classname(base, tablename, table):
  145. "Produce a 'camelized' class name, e.g. "
  146. "'words_and_underscores' -> 'WordsAndUnderscores'"
  147. return str(tablename[0].upper() + \
  148. re.sub(r'_([a-z])', lambda m: m.group(1).upper(), tablename[1:]))
  149. _pluralizer = inflect.engine()
  150. def pluralize_collection(base, local_cls, referred_cls, constraint):
  151. "Produce an 'uncamelized', 'pluralized' class name, e.g. "
  152. "'SomeTerm' -> 'some_terms'"
  153. referred_name = referred_cls.__name__
  154. uncamelized = re.sub(r'[A-Z]',
  155. lambda m: "_%s" % m.group(0).lower(),
  156. referred_name)[1:]
  157. pluralized = _pluralizer.plural(uncamelized)
  158. return pluralized
  159. from sqlalchemy.ext.automap import automap_base
  160. Base = automap_base()
  161. engine = create_engine("sqlite:///mydatabase.db")
  162. Base.prepare(engine, reflect=True,
  163. classname_for_table=camelize_classname,
  164. name_for_collection_relationship=pluralize_collection
  165. )
  166. From the above mapping, we would now have classes ``User`` and ``Address``,
  167. where the collection from ``User`` to ``Address`` is called
  168. ``User.addresses``::
  169. User, Address = Base.classes.User, Base.classes.Address
  170. u1 = User(addresses=[Address(email="foo@bar.com")])
  171. Relationship Detection
  172. ======================
  173. The vast majority of what automap accomplishes is the generation of
  174. :func:`.relationship` structures based on foreign keys. The mechanism
  175. by which this works for many-to-one and one-to-many relationships is as
  176. follows:
  177. 1. A given :class:`.Table`, known to be mapped to a particular class,
  178. is examined for :class:`.ForeignKeyConstraint` objects.
  179. 2. From each :class:`.ForeignKeyConstraint`, the remote :class:`.Table`
  180. object present is matched up to the class to which it is to be mapped,
  181. if any, else it is skipped.
  182. 3. As the :class:`.ForeignKeyConstraint` we are examining corresponds to a
  183. reference from the immediate mapped class, the relationship will be set up
  184. as a many-to-one referring to the referred class; a corresponding
  185. one-to-many backref will be created on the referred class referring
  186. to this class.
  187. 4. If any of the columns that are part of the :class:`.ForeignKeyConstraint`
  188. are not nullable (e.g. ``nullable=False``), a
  189. :paramref:`~.relationship.cascade` keyword argument
  190. of ``all, delete-orphan`` will be added to the keyword arguments to
  191. be passed to the relationship or backref. If the
  192. :class:`.ForeignKeyConstraint` reports that
  193. :paramref:`.ForeignKeyConstraint.ondelete`
  194. is set to ``CASCADE`` for a not null or ``SET NULL`` for a nullable
  195. set of columns, the option :paramref:`~.relationship.passive_deletes`
  196. flag is set to ``True`` in the set of relationship keyword arguments.
  197. Note that not all backends support reflection of ON DELETE.
  198. .. versionadded:: 1.0.0 - automap will detect non-nullable foreign key
  199. constraints when producing a one-to-many relationship and establish
  200. a default cascade of ``all, delete-orphan`` if so; additionally,
  201. if the constraint specifies :paramref:`.ForeignKeyConstraint.ondelete`
  202. of ``CASCADE`` for non-nullable or ``SET NULL`` for nullable columns,
  203. the ``passive_deletes=True`` option is also added.
  204. 5. The names of the relationships are determined using the
  205. :paramref:`.AutomapBase.prepare.name_for_scalar_relationship` and
  206. :paramref:`.AutomapBase.prepare.name_for_collection_relationship`
  207. callable functions. It is important to note that the default relationship
  208. naming derives the name from the **the actual class name**. If you've
  209. given a particular class an explicit name by declaring it, or specified an
  210. alternate class naming scheme, that's the name from which the relationship
  211. name will be derived.
  212. 6. The classes are inspected for an existing mapped property matching these
  213. names. If one is detected on one side, but none on the other side,
  214. :class:`.AutomapBase` attempts to create a relationship on the missing side,
  215. then uses the :paramref:`.relationship.back_populates` parameter in order to
  216. point the new relationship to the other side.
  217. 7. In the usual case where no relationship is on either side,
  218. :meth:`.AutomapBase.prepare` produces a :func:`.relationship` on the
  219. "many-to-one" side and matches it to the other using the
  220. :paramref:`.relationship.backref` parameter.
  221. 8. Production of the :func:`.relationship` and optionally the :func:`.backref`
  222. is handed off to the :paramref:`.AutomapBase.prepare.generate_relationship`
  223. function, which can be supplied by the end-user in order to augment
  224. the arguments passed to :func:`.relationship` or :func:`.backref` or to
  225. make use of custom implementations of these functions.
  226. Custom Relationship Arguments
  227. -----------------------------
  228. The :paramref:`.AutomapBase.prepare.generate_relationship` hook can be used
  229. to add parameters to relationships. For most cases, we can make use of the
  230. existing :func:`.automap.generate_relationship` function to return
  231. the object, after augmenting the given keyword dictionary with our own
  232. arguments.
  233. Below is an illustration of how to send
  234. :paramref:`.relationship.cascade` and
  235. :paramref:`.relationship.passive_deletes`
  236. options along to all one-to-many relationships::
  237. from sqlalchemy.ext.automap import generate_relationship
  238. def _gen_relationship(base, direction, return_fn,
  239. attrname, local_cls, referred_cls, **kw):
  240. if direction is interfaces.ONETOMANY:
  241. kw['cascade'] = 'all, delete-orphan'
  242. kw['passive_deletes'] = True
  243. # make use of the built-in function to actually return
  244. # the result.
  245. return generate_relationship(base, direction, return_fn,
  246. attrname, local_cls, referred_cls, **kw)
  247. from sqlalchemy.ext.automap import automap_base
  248. from sqlalchemy import create_engine
  249. # automap base
  250. Base = automap_base()
  251. engine = create_engine("sqlite:///mydatabase.db")
  252. Base.prepare(engine, reflect=True,
  253. generate_relationship=_gen_relationship)
  254. Many-to-Many relationships
  255. --------------------------
  256. :mod:`.sqlalchemy.ext.automap` will generate many-to-many relationships, e.g.
  257. those which contain a ``secondary`` argument. The process for producing these
  258. is as follows:
  259. 1. A given :class:`.Table` is examined for :class:`.ForeignKeyConstraint`
  260. objects, before any mapped class has been assigned to it.
  261. 2. If the table contains two and exactly two :class:`.ForeignKeyConstraint`
  262. objects, and all columns within this table are members of these two
  263. :class:`.ForeignKeyConstraint` objects, the table is assumed to be a
  264. "secondary" table, and will **not be mapped directly**.
  265. 3. The two (or one, for self-referential) external tables to which the
  266. :class:`.Table` refers to are matched to the classes to which they will be
  267. mapped, if any.
  268. 4. If mapped classes for both sides are located, a many-to-many bi-directional
  269. :func:`.relationship` / :func:`.backref` pair is created between the two
  270. classes.
  271. 5. The override logic for many-to-many works the same as that of one-to-many/
  272. many-to-one; the :func:`.generate_relationship` function is called upon
  273. to generate the strucures and existing attributes will be maintained.
  274. Relationships with Inheritance
  275. ------------------------------
  276. :mod:`.sqlalchemy.ext.automap` will not generate any relationships between
  277. two classes that are in an inheritance relationship. That is, with two
  278. classes given as follows::
  279. class Employee(Base):
  280. __tablename__ = 'employee'
  281. id = Column(Integer, primary_key=True)
  282. type = Column(String(50))
  283. __mapper_args__ = {
  284. 'polymorphic_identity':'employee', 'polymorphic_on': type
  285. }
  286. class Engineer(Employee):
  287. __tablename__ = 'engineer'
  288. id = Column(Integer, ForeignKey('employee.id'), primary_key=True)
  289. __mapper_args__ = {
  290. 'polymorphic_identity':'engineer',
  291. }
  292. The foreign key from ``Engineer`` to ``Employee`` is used not for a
  293. relationship, but to establish joined inheritance between the two classes.
  294. Note that this means automap will not generate *any* relationships
  295. for foreign keys that link from a subclass to a superclass. If a mapping
  296. has actual relationships from subclass to superclass as well, those
  297. need to be explicit. Below, as we have two separate foreign keys
  298. from ``Engineer`` to ``Employee``, we need to set up both the relationship
  299. we want as well as the ``inherit_condition``, as these are not things
  300. SQLAlchemy can guess::
  301. class Employee(Base):
  302. __tablename__ = 'employee'
  303. id = Column(Integer, primary_key=True)
  304. type = Column(String(50))
  305. __mapper_args__ = {
  306. 'polymorphic_identity':'employee', 'polymorphic_on':type
  307. }
  308. class Engineer(Employee):
  309. __tablename__ = 'engineer'
  310. id = Column(Integer, ForeignKey('employee.id'), primary_key=True)
  311. favorite_employee_id = Column(Integer, ForeignKey('employee.id'))
  312. favorite_employee = relationship(Employee,
  313. foreign_keys=favorite_employee_id)
  314. __mapper_args__ = {
  315. 'polymorphic_identity':'engineer',
  316. 'inherit_condition': id == Employee.id
  317. }
  318. Handling Simple Naming Conflicts
  319. --------------------------------
  320. In the case of naming conflicts during mapping, override any of
  321. :func:`.classname_for_table`, :func:`.name_for_scalar_relationship`,
  322. and :func:`.name_for_collection_relationship` as needed. For example, if
  323. automap is attempting to name a many-to-one relationship the same as an
  324. existing column, an alternate convention can be conditionally selected. Given
  325. a schema:
  326. .. sourcecode:: sql
  327. CREATE TABLE table_a (
  328. id INTEGER PRIMARY KEY
  329. );
  330. CREATE TABLE table_b (
  331. id INTEGER PRIMARY KEY,
  332. table_a INTEGER,
  333. FOREIGN KEY(table_a) REFERENCES table_a(id)
  334. );
  335. The above schema will first automap the ``table_a`` table as a class named
  336. ``table_a``; it will then automap a relationship onto the class for ``table_b``
  337. with the same name as this related class, e.g. ``table_a``. This
  338. relationship name conflicts with the mapping column ``table_b.table_a``,
  339. and will emit an error on mapping.
  340. We can resolve this conflict by using an underscore as follows::
  341. def name_for_scalar_relationship(base, local_cls, referred_cls, constraint):
  342. name = referred_cls.__name__.lower()
  343. local_table = local_cls.__table__
  344. if name in local_table.columns:
  345. newname = name + "_"
  346. warnings.warn(
  347. "Already detected name %s present. using %s" %
  348. (name, newname))
  349. return newname
  350. return name
  351. Base.prepare(engine, reflect=True,
  352. name_for_scalar_relationship=name_for_scalar_relationship)
  353. Alternatively, we can change the name on the column side. The columns
  354. that are mapped can be modified using the technique described at
  355. :ref:`mapper_column_distinct_names`, by assigning the column explicitly
  356. to a new name::
  357. Base = automap_base()
  358. class TableB(Base):
  359. __tablename__ = 'table_b'
  360. _table_a = Column('table_a', ForeignKey('table_a.id'))
  361. Base.prepare(engine, reflect=True)
  362. Using Automap with Explicit Declarations
  363. ========================================
  364. As noted previously, automap has no dependency on reflection, and can make
  365. use of any collection of :class:`.Table` objects within a :class:`.MetaData`
  366. collection. From this, it follows that automap can also be used
  367. generate missing relationships given an otherwise complete model that fully
  368. defines table metadata::
  369. from sqlalchemy.ext.automap import automap_base
  370. from sqlalchemy import Column, Integer, String, ForeignKey
  371. Base = automap_base()
  372. class User(Base):
  373. __tablename__ = 'user'
  374. id = Column(Integer, primary_key=True)
  375. name = Column(String)
  376. class Address(Base):
  377. __tablename__ = 'address'
  378. id = Column(Integer, primary_key=True)
  379. email = Column(String)
  380. user_id = Column(ForeignKey('user.id'))
  381. # produce relationships
  382. Base.prepare()
  383. # mapping is complete, with "address_collection" and
  384. # "user" relationships
  385. a1 = Address(email='u1')
  386. a2 = Address(email='u2')
  387. u1 = User(address_collection=[a1, a2])
  388. assert a1.user is u1
  389. Above, given mostly complete ``User`` and ``Address`` mappings, the
  390. :class:`.ForeignKey` which we defined on ``Address.user_id`` allowed a
  391. bidirectional relationship pair ``Address.user`` and
  392. ``User.address_collection`` to be generated on the mapped classes.
  393. Note that when subclassing :class:`.AutomapBase`,
  394. the :meth:`.AutomapBase.prepare` method is required; if not called, the classes
  395. we've declared are in an un-mapped state.
  396. """
  397. from .declarative import declarative_base as _declarative_base
  398. from .declarative.base import _DeferredMapperConfig
  399. from ..sql import and_
  400. from ..schema import ForeignKeyConstraint
  401. from ..orm import relationship, backref, interfaces
  402. from .. import util
  403. def classname_for_table(base, tablename, table):
  404. """Return the class name that should be used, given the name
  405. of a table.
  406. The default implementation is::
  407. return str(tablename)
  408. Alternate implementations can be specified using the
  409. :paramref:`.AutomapBase.prepare.classname_for_table`
  410. parameter.
  411. :param base: the :class:`.AutomapBase` class doing the prepare.
  412. :param tablename: string name of the :class:`.Table`.
  413. :param table: the :class:`.Table` object itself.
  414. :return: a string class name.
  415. .. note::
  416. In Python 2, the string used for the class name **must** be a
  417. non-Unicode object, e.g. a ``str()`` object. The ``.name`` attribute
  418. of :class:`.Table` is typically a Python unicode subclass, so the
  419. ``str()`` function should be applied to this name, after accounting for
  420. any non-ASCII characters.
  421. """
  422. return str(tablename)
  423. def name_for_scalar_relationship(base, local_cls, referred_cls, constraint):
  424. """Return the attribute name that should be used to refer from one
  425. class to another, for a scalar object reference.
  426. The default implementation is::
  427. return referred_cls.__name__.lower()
  428. Alternate implementations can be specified using the
  429. :paramref:`.AutomapBase.prepare.name_for_scalar_relationship`
  430. parameter.
  431. :param base: the :class:`.AutomapBase` class doing the prepare.
  432. :param local_cls: the class to be mapped on the local side.
  433. :param referred_cls: the class to be mapped on the referring side.
  434. :param constraint: the :class:`.ForeignKeyConstraint` that is being
  435. inspected to produce this relationship.
  436. """
  437. return referred_cls.__name__.lower()
  438. def name_for_collection_relationship(
  439. base, local_cls, referred_cls, constraint):
  440. """Return the attribute name that should be used to refer from one
  441. class to another, for a collection reference.
  442. The default implementation is::
  443. return referred_cls.__name__.lower() + "_collection"
  444. Alternate implementations
  445. can be specified using the
  446. :paramref:`.AutomapBase.prepare.name_for_collection_relationship`
  447. parameter.
  448. :param base: the :class:`.AutomapBase` class doing the prepare.
  449. :param local_cls: the class to be mapped on the local side.
  450. :param referred_cls: the class to be mapped on the referring side.
  451. :param constraint: the :class:`.ForeignKeyConstraint` that is being
  452. inspected to produce this relationship.
  453. """
  454. return referred_cls.__name__.lower() + "_collection"
  455. def generate_relationship(
  456. base, direction, return_fn, attrname, local_cls, referred_cls, **kw):
  457. r"""Generate a :func:`.relationship` or :func:`.backref` on behalf of two
  458. mapped classes.
  459. An alternate implementation of this function can be specified using the
  460. :paramref:`.AutomapBase.prepare.generate_relationship` parameter.
  461. The default implementation of this function is as follows::
  462. if return_fn is backref:
  463. return return_fn(attrname, **kw)
  464. elif return_fn is relationship:
  465. return return_fn(referred_cls, **kw)
  466. else:
  467. raise TypeError("Unknown relationship function: %s" % return_fn)
  468. :param base: the :class:`.AutomapBase` class doing the prepare.
  469. :param direction: indicate the "direction" of the relationship; this will
  470. be one of :data:`.ONETOMANY`, :data:`.MANYTOONE`, :data:`.MANYTOMANY`.
  471. :param return_fn: the function that is used by default to create the
  472. relationship. This will be either :func:`.relationship` or
  473. :func:`.backref`. The :func:`.backref` function's result will be used to
  474. produce a new :func:`.relationship` in a second step, so it is critical
  475. that user-defined implementations correctly differentiate between the two
  476. functions, if a custom relationship function is being used.
  477. :param attrname: the attribute name to which this relationship is being
  478. assigned. If the value of :paramref:`.generate_relationship.return_fn` is
  479. the :func:`.backref` function, then this name is the name that is being
  480. assigned to the backref.
  481. :param local_cls: the "local" class to which this relationship or backref
  482. will be locally present.
  483. :param referred_cls: the "referred" class to which the relationship or
  484. backref refers to.
  485. :param \**kw: all additional keyword arguments are passed along to the
  486. function.
  487. :return: a :func:`.relationship` or :func:`.backref` construct, as dictated
  488. by the :paramref:`.generate_relationship.return_fn` parameter.
  489. """
  490. if return_fn is backref:
  491. return return_fn(attrname, **kw)
  492. elif return_fn is relationship:
  493. return return_fn(referred_cls, **kw)
  494. else:
  495. raise TypeError("Unknown relationship function: %s" % return_fn)
  496. class AutomapBase(object):
  497. """Base class for an "automap" schema.
  498. The :class:`.AutomapBase` class can be compared to the "declarative base"
  499. class that is produced by the :func:`.declarative.declarative_base`
  500. function. In practice, the :class:`.AutomapBase` class is always used
  501. as a mixin along with an actual declarative base.
  502. A new subclassable :class:`.AutomapBase` is typically instantated
  503. using the :func:`.automap_base` function.
  504. .. seealso::
  505. :ref:`automap_toplevel`
  506. """
  507. __abstract__ = True
  508. classes = None
  509. """An instance of :class:`.util.Properties` containing classes.
  510. This object behaves much like the ``.c`` collection on a table. Classes
  511. are present under the name they were given, e.g.::
  512. Base = automap_base()
  513. Base.prepare(engine=some_engine, reflect=True)
  514. User, Address = Base.classes.User, Base.classes.Address
  515. """
  516. @classmethod
  517. def prepare(
  518. cls,
  519. engine=None,
  520. reflect=False,
  521. schema=None,
  522. classname_for_table=classname_for_table,
  523. collection_class=list,
  524. name_for_scalar_relationship=name_for_scalar_relationship,
  525. name_for_collection_relationship=name_for_collection_relationship,
  526. generate_relationship=generate_relationship):
  527. """Extract mapped classes and relationships from the :class:`.MetaData` and
  528. perform mappings.
  529. :param engine: an :class:`.Engine` or :class:`.Connection` with which
  530. to perform schema reflection, if specified.
  531. If the :paramref:`.AutomapBase.prepare.reflect` argument is False,
  532. this object is not used.
  533. :param reflect: if True, the :meth:`.MetaData.reflect` method is called
  534. on the :class:`.MetaData` associated with this :class:`.AutomapBase`.
  535. The :class:`.Engine` passed via
  536. :paramref:`.AutomapBase.prepare.engine` will be used to perform the
  537. reflection if present; else, the :class:`.MetaData` should already be
  538. bound to some engine else the operation will fail.
  539. :param classname_for_table: callable function which will be used to
  540. produce new class names, given a table name. Defaults to
  541. :func:`.classname_for_table`.
  542. :param name_for_scalar_relationship: callable function which will be
  543. used to produce relationship names for scalar relationships. Defaults
  544. to :func:`.name_for_scalar_relationship`.
  545. :param name_for_collection_relationship: callable function which will
  546. be used to produce relationship names for collection-oriented
  547. relationships. Defaults to :func:`.name_for_collection_relationship`.
  548. :param generate_relationship: callable function which will be used to
  549. actually generate :func:`.relationship` and :func:`.backref`
  550. constructs. Defaults to :func:`.generate_relationship`.
  551. :param collection_class: the Python collection class that will be used
  552. when a new :func:`.relationship` object is created that represents a
  553. collection. Defaults to ``list``.
  554. :param schema: When present in conjunction with the
  555. :paramref:`.AutomapBase.prepare.reflect` flag, is passed to
  556. :meth:`.MetaData.reflect` to indicate the primary schema where tables
  557. should be reflected from. When omitted, the default schema in use
  558. by the database connection is used.
  559. .. versionadded:: 1.1
  560. """
  561. if reflect:
  562. cls.metadata.reflect(
  563. engine,
  564. schema=schema,
  565. extend_existing=True,
  566. autoload_replace=False
  567. )
  568. table_to_map_config = dict(
  569. (m.local_table, m)
  570. for m in _DeferredMapperConfig.
  571. classes_for_base(cls, sort=False)
  572. )
  573. many_to_many = []
  574. for table in cls.metadata.tables.values():
  575. lcl_m2m, rem_m2m, m2m_const = _is_many_to_many(cls, table)
  576. if lcl_m2m is not None:
  577. many_to_many.append((lcl_m2m, rem_m2m, m2m_const, table))
  578. elif not table.primary_key:
  579. continue
  580. elif table not in table_to_map_config:
  581. mapped_cls = type(
  582. classname_for_table(cls, table.name, table),
  583. (cls, ),
  584. {"__table__": table}
  585. )
  586. map_config = _DeferredMapperConfig.config_for_cls(mapped_cls)
  587. cls.classes[map_config.cls.__name__] = mapped_cls
  588. table_to_map_config[table] = map_config
  589. for map_config in table_to_map_config.values():
  590. _relationships_for_fks(cls,
  591. map_config,
  592. table_to_map_config,
  593. collection_class,
  594. name_for_scalar_relationship,
  595. name_for_collection_relationship,
  596. generate_relationship)
  597. for lcl_m2m, rem_m2m, m2m_const, table in many_to_many:
  598. _m2m_relationship(cls, lcl_m2m, rem_m2m, m2m_const, table,
  599. table_to_map_config,
  600. collection_class,
  601. name_for_scalar_relationship,
  602. name_for_collection_relationship,
  603. generate_relationship)
  604. for map_config in _DeferredMapperConfig.classes_for_base(cls):
  605. map_config.map()
  606. _sa_decl_prepare = True
  607. """Indicate that the mapping of classes should be deferred.
  608. The presence of this attribute name indicates to declarative
  609. that the call to mapper() should not occur immediately; instead,
  610. information about the table and attributes to be mapped are gathered
  611. into an internal structure called _DeferredMapperConfig. These
  612. objects can be collected later using classes_for_base(), additional
  613. mapping decisions can be made, and then the map() method will actually
  614. apply the mapping.
  615. The only real reason this deferral of the whole
  616. thing is needed is to support primary key columns that aren't reflected
  617. yet when the class is declared; everything else can theoretically be
  618. added to the mapper later. However, the _DeferredMapperConfig is a
  619. nice interface in any case which exists at that not usually exposed point
  620. at which declarative has the class and the Table but hasn't called
  621. mapper() yet.
  622. """
  623. def automap_base(declarative_base=None, **kw):
  624. r"""Produce a declarative automap base.
  625. This function produces a new base class that is a product of the
  626. :class:`.AutomapBase` class as well a declarative base produced by
  627. :func:`.declarative.declarative_base`.
  628. All parameters other than ``declarative_base`` are keyword arguments
  629. that are passed directly to the :func:`.declarative.declarative_base`
  630. function.
  631. :param declarative_base: an existing class produced by
  632. :func:`.declarative.declarative_base`. When this is passed, the function
  633. no longer invokes :func:`.declarative.declarative_base` itself, and all
  634. other keyword arguments are ignored.
  635. :param \**kw: keyword arguments are passed along to
  636. :func:`.declarative.declarative_base`.
  637. """
  638. if declarative_base is None:
  639. Base = _declarative_base(**kw)
  640. else:
  641. Base = declarative_base
  642. return type(
  643. Base.__name__,
  644. (AutomapBase, Base,),
  645. {"__abstract__": True, "classes": util.Properties({})}
  646. )
  647. def _is_many_to_many(automap_base, table):
  648. fk_constraints = [const for const in table.constraints
  649. if isinstance(const, ForeignKeyConstraint)]
  650. if len(fk_constraints) != 2:
  651. return None, None, None
  652. cols = sum(
  653. [[fk.parent for fk in fk_constraint.elements]
  654. for fk_constraint in fk_constraints], [])
  655. if set(cols) != set(table.c):
  656. return None, None, None
  657. return (
  658. fk_constraints[0].elements[0].column.table,
  659. fk_constraints[1].elements[0].column.table,
  660. fk_constraints
  661. )
  662. def _relationships_for_fks(automap_base, map_config, table_to_map_config,
  663. collection_class,
  664. name_for_scalar_relationship,
  665. name_for_collection_relationship,
  666. generate_relationship):
  667. local_table = map_config.local_table
  668. local_cls = map_config.cls # derived from a weakref, may be None
  669. if local_table is None or local_cls is None:
  670. return
  671. for constraint in local_table.constraints:
  672. if isinstance(constraint, ForeignKeyConstraint):
  673. fks = constraint.elements
  674. referred_table = fks[0].column.table
  675. referred_cfg = table_to_map_config.get(referred_table, None)
  676. if referred_cfg is None:
  677. continue
  678. referred_cls = referred_cfg.cls
  679. if local_cls is not referred_cls and issubclass(
  680. local_cls, referred_cls):
  681. continue
  682. relationship_name = name_for_scalar_relationship(
  683. automap_base,
  684. local_cls,
  685. referred_cls, constraint)
  686. backref_name = name_for_collection_relationship(
  687. automap_base,
  688. referred_cls,
  689. local_cls,
  690. constraint
  691. )
  692. o2m_kws = {}
  693. nullable = False not in set([fk.parent.nullable for fk in fks])
  694. if not nullable:
  695. o2m_kws['cascade'] = "all, delete-orphan"
  696. if constraint.ondelete and \
  697. constraint.ondelete.lower() == "cascade":
  698. o2m_kws['passive_deletes'] = True
  699. else:
  700. if constraint.ondelete and \
  701. constraint.ondelete.lower() == "set null":
  702. o2m_kws['passive_deletes'] = True
  703. create_backref = backref_name not in referred_cfg.properties
  704. if relationship_name not in map_config.properties:
  705. if create_backref:
  706. backref_obj = generate_relationship(
  707. automap_base,
  708. interfaces.ONETOMANY, backref,
  709. backref_name, referred_cls, local_cls,
  710. collection_class=collection_class,
  711. **o2m_kws)
  712. else:
  713. backref_obj = None
  714. rel = generate_relationship(automap_base,
  715. interfaces.MANYTOONE,
  716. relationship,
  717. relationship_name,
  718. local_cls, referred_cls,
  719. foreign_keys=[
  720. fk.parent
  721. for fk in constraint.elements],
  722. backref=backref_obj,
  723. remote_side=[
  724. fk.column
  725. for fk in constraint.elements]
  726. )
  727. if rel is not None:
  728. map_config.properties[relationship_name] = rel
  729. if not create_backref:
  730. referred_cfg.properties[
  731. backref_name].back_populates = relationship_name
  732. elif create_backref:
  733. rel = generate_relationship(automap_base,
  734. interfaces.ONETOMANY,
  735. relationship,
  736. backref_name,
  737. referred_cls, local_cls,
  738. foreign_keys=[
  739. fk.parent
  740. for fk in constraint.elements],
  741. back_populates=relationship_name,
  742. collection_class=collection_class,
  743. **o2m_kws)
  744. if rel is not None:
  745. referred_cfg.properties[backref_name] = rel
  746. map_config.properties[
  747. relationship_name].back_populates = backref_name
  748. def _m2m_relationship(automap_base, lcl_m2m, rem_m2m, m2m_const, table,
  749. table_to_map_config,
  750. collection_class,
  751. name_for_scalar_relationship,
  752. name_for_collection_relationship,
  753. generate_relationship):
  754. map_config = table_to_map_config.get(lcl_m2m, None)
  755. referred_cfg = table_to_map_config.get(rem_m2m, None)
  756. if map_config is None or referred_cfg is None:
  757. return
  758. local_cls = map_config.cls
  759. referred_cls = referred_cfg.cls
  760. relationship_name = name_for_collection_relationship(
  761. automap_base,
  762. local_cls,
  763. referred_cls, m2m_const[0])
  764. backref_name = name_for_collection_relationship(
  765. automap_base,
  766. referred_cls,
  767. local_cls,
  768. m2m_const[1]
  769. )
  770. create_backref = backref_name not in referred_cfg.properties
  771. if relationship_name not in map_config.properties:
  772. if create_backref:
  773. backref_obj = generate_relationship(
  774. automap_base,
  775. interfaces.MANYTOMANY,
  776. backref,
  777. backref_name,
  778. referred_cls, local_cls,
  779. collection_class=collection_class
  780. )
  781. else:
  782. backref_obj = None
  783. rel = generate_relationship(automap_base,
  784. interfaces.MANYTOMANY,
  785. relationship,
  786. relationship_name,
  787. local_cls, referred_cls,
  788. secondary=table,
  789. primaryjoin=and_(
  790. fk.column == fk.parent
  791. for fk in m2m_const[0].elements),
  792. secondaryjoin=and_(
  793. fk.column == fk.parent
  794. for fk in m2m_const[1].elements),
  795. backref=backref_obj,
  796. collection_class=collection_class
  797. )
  798. if rel is not None:
  799. map_config.properties[relationship_name] = rel
  800. if not create_backref:
  801. referred_cfg.properties[
  802. backref_name].back_populates = relationship_name
  803. elif create_backref:
  804. rel = generate_relationship(automap_base,
  805. interfaces.MANYTOMANY,
  806. relationship,
  807. backref_name,
  808. referred_cls, local_cls,
  809. secondary=table,
  810. primaryjoin=and_(
  811. fk.column == fk.parent
  812. for fk in m2m_const[1].elements),
  813. secondaryjoin=and_(
  814. fk.column == fk.parent
  815. for fk in m2m_const[0].elements),
  816. back_populates=relationship_name,
  817. collection_class=collection_class)
  818. if rel is not None:
  819. referred_cfg.properties[backref_name] = rel
  820. map_config.properties[
  821. relationship_name].back_populates = backref_name