automap.py 45 KB

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