summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/orm
Commit message (Collapse)AuthorAgeFilesLines
...
* Merge "fixes for mypy 0.971" into mainmike bayer2022-07-193-3/+3
|\
| * fixes for mypy 0.971Mike Bayer2022-07-193-3/+3
| | | | | | | | | | | | | | | | | | | | | | things that were passing with 0.961 need adjustment. it seems mypy has become very pedantic about the difference between importing from a module vs. accessing members of that module as instance variables, so adjust the preloaded typing block to be explicitly instance variables, since that's how the accessor works in any case. Change-Id: I746a3c9102530b7cf9b123aec7be6376657c1169
* | Ensure that a daclarative base is not used directlyFederico Caselli2022-07-181-10/+24
|/ | | | | Fixes: #8248 Change-Id: I4f4c690dd8659eaf74e9c757d681e9edc7d33eee
* Merge "implement column._merge()" into mainmike bayer2022-07-172-25/+15
|\
| * implement column._merge()Mike Bayer2022-07-162-25/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | this takes the user-defined args of one Column and merges them into the not-user-defined args of another Column. Implemented within the pep-593 column transfer operation to begin to make this new feature more robust. work may still be needed for constraints etc. but in theory everything from the left side annotated column should take effect for the right side if not otherwise specified on the right. Change-Id: I57eb37ed6ceb4b60979a35cfc4b63731d990911d
* | update ORM declarative docs for new featuresMike Bayer2022-07-167-413/+430
|/ | | | | | | I screwed up a rebase or something so this was temporarily in Ic51a12de3358f3a451bd7cf3542b375569499fc1 Change-Id: I847ee1336381221c0112b67854df022edf596b25
* make anno-only Mapped[] column available for mixinsMike Bayer2022-07-152-22/+36
| | | | | | | | | | | | | | Documentation is relying on the recently improved behavior of produce_column_copies() to make sure everything is available on cls for a declared_attr. therefore for anno-only attribute, we also need to generate the mapped_column() up front before scan is called. noticed in pylance, allow @declared_attr to recognize @classmethod also which allows letting typing tools know something is explicitly a classmethod Change-Id: I07ff1a642a75679f685914a33c674807929f4918
* Minor cleanupFederico Caselli2022-07-131-1/+2
| | | | | | | - remove unnecessary postgresql visit that's equal to the default compiler - clarify type_annotation_map documentation Change-Id: I0c1fa212d06f6af799a5894802574250622c855e
* implement comparison ops for compositesMike Bayer2022-07-131-6/+33
| | | | | | | classes mapped by :class:`_orm.composite` now support ordering comparison operations, e.g. ``<``, ``>=``, etc. Change-Id: I44938b9ca2935b2f63c70e930768487ddc6b7669
* support "SELECT *" for ORM queriesMike Bayer2022-07-101-0/+12
| | | | | | | | | | | | | | | | | | A :func:`_sql.select` construct that is passed a sole '*' argument for ``SELECT *``, either via string, :func:`_sql.text`, or :func:`_sql.literal_column`, will be interpreted as a Core-level SQL statement rather than as an ORM level statement. This is so that the ``*``, when expanded to match any number of columns, will result in all columns returned in the result. the ORM- level interpretation of :func:`_sql.select` needs to know the names and types of all ORM columns up front which can't be achieved when ``'*'`` is used. If ``'*`` is used amongst other expressions simultaneously with an ORM statement, an error is raised as this can't be interpreted correctly by the ORM. Fixes: #8235 Change-Id: Ic8e84491e14acdc8570704eadeaeaf6e16b1e870
* move backref to "legacy"Mike Bayer2022-07-041-26/+41
| | | | | | | | | | | in the interests of consistency as well as new typing features, backref should be considered legacy and is fully superseded by back_populates. this commit is for 2.0 /1.4, in 2.0 further updates will be made for new ORM syntaxes. Change-Id: Idd3b7a3b07843b73304df69e476dc4239c60b3f8
* runtime annotation fixes for relationshipMike Bayer2022-07-034-8/+38
| | | | | | | | | | | | | | | | * derive uselist=False when fwd ref passed to relationship This case needs to work whether or not the class name is a forward ref. we dont allow the colleciton to be a forward ref so this will work. * fix issues with MappedCollection When using string annotations or __future__.annotations, we need to do more parsing in order to get the target collection properly Change-Id: I9e5a1358b62d060a8815826f98190801a9cc0b68
* call toinstance() on type arguments passed to mapped_column()Mike Bayer2022-07-021-1/+2
| | | | Change-Id: I875cfbd925cb08e0a5235f87d13341d319c955bc
* repair yield_per for non-SS dialects and add new optionsMike Bayer2022-07-012-7/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | Implemented new :paramref:`_engine.Connection.execution_options.yield_per` execution option for :class:`_engine.Connection` in Core, to mirror that of the same :ref:`yield_per <orm_queryguide_yield_per>` option available in the ORM. The option sets both the :paramref:`_engine.Connection.execution_options.stream_results` option at the same time as invoking :meth:`_engine.Result.yield_per`, to provide the most common streaming result configuration which also mirrors that of the ORM use case in its usage pattern. Fixed bug in :class:`_engine.Result` where the usage of a buffered result strategy would not be used if the dialect in use did not support an explicit "server side cursor" setting, when using :paramref:`_engine.Connection.execution_options.stream_results`. This is in error as DBAPIs such as that of SQLite and Oracle already use a non-buffered result fetching scheme, which still benefits from usage of partial result fetching. The "buffered" strategy is now used in all cases where :paramref:`_engine.Connection.execution_options.stream_results` is set. Added :meth:`.FilterResult.yield_per` so that result implementations such as :class:`.MappingResult`, :class:`.ScalarResult` and :class:`.AsyncResult` have access to this method. Fixes: #8199 Change-Id: I6dde3cbe483a1bf81e945561b60f4b7d1c434750
* Merge "produce column copies up the whole hierarchy first" into mainmike bayer2022-06-291-4/+37
|\
| * produce column copies up the whole hierarchy firstMike Bayer2022-06-291-4/+37
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Fixed issue where a hierarchy of classes set up as an abstract or mixin declarative classes could not declare standalone columns on a superclass that would then be copied correctly to a :class:`_orm.declared_attr` callable that wanted to make use of them on a descendant class. Originally it looked like this would produce an ordering change, however an adjustment to the flow for produce_column_copies has avoided that for now. Fixes: #8190 Change-Id: I4e2ee74edb110793eb42691c3e4a0e0535fba7e9
* | merge column args from Annotated left sideMike Bayer2022-06-282-12/+58
|/ | | | | | | | | | | | because we are forced by pep-681 to use the argument "default", we need a way to have client Column default separate from a dataclasses level default. Also, pep-681 does not support deriving the descriptor function from Annotated, so allow a brief right side mapped_column() to be present that will have more column-centric arguments from the left side Annotated to be merged. Change-Id: I039be1628d498486ba013b2798e1392ed1cd7f9f
* refine _include_fn to not include sibling mappersMike Bayer2022-06-231-1/+2
| | | | | | | | | | | Fixed regression caused by :ticket:`8064` where a particular check for column correspondence was made too liberal, resulting in incorrect rendering for some ORM subqueries such as those using :meth:`.PropComparator.has` or :meth:`.PropComparator.any` in conjunction with joined-inheritance queries that also use legacy aliasing features. Fixes: #8162 Change-Id: Ib1fff33aa219aadf178348dd571bec1e691e606d
* rework ORM mapping docsMike Bayer2022-06-212-5/+35
| | | | | | | | | | prepare docs for newly incoming mapper styles, including new dataclass mapping. move the existing dataclass/attrs docs all into their own section and try to improve organization and wording into the relatively recent "mapping styles" document. Change-Id: I0b5e2a5b6a70db65ab19b5bb0a2bb7df20e0b498
* Merge "create new approach for deeply nested post loader options" into mainmike bayer2022-06-185-72/+393
|\
| * create new approach for deeply nested post loader optionsMike Bayer2022-06-185-72/+393
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Added very experimental feature to the :func:`_orm.selectinload` and :func:`_orm.immediateload` loader options called :paramref:`_orm.selectinload.recursion_depth` / :paramref:`_orm.immediateload.recursion_depth` , which allows a single loader option to automatically recurse into self-referential relationships. Is set to an integer indicating depth, and may also be set to -1 to indicate to continue loading until no more levels deep are found. Major internal changes to :func:`_orm.selectinload` and :func:`_orm.immediateload` allow this feature to work while continuing to make correct use of the compilation cache, as well as not using arbitrary recursion, so any level of depth is supported (though would emit that many queries). This may be useful for self-referential structures that must be loaded fully eagerly, such as when using asyncio. A warning is also emitted when loader options are connected together with arbitrary lengths (that is, without using the new ``recursion_depth`` option) when excessive recursion depth is detected in related object loading. This operation continues to use huge amounts of memory and performs extremely poorly; the cache is disabled when this condition is detected to protect the cache from being flooded with arbitrary statements. Fixes: #8126 Change-Id: I9f162e0a09c1ed327dd19498aac193f649333a01
* | Normalize postgresql docs links to point to currentFederico Caselli2022-06-171-1/+1
|/ | | | Change-Id: Ib7d3ea7ff3356ff8a2f935892d904a69dbc25c3e
* Revert "add auto_recurse option to selectinload, immediateload"Mike Bayer2022-06-162-93/+10
| | | | | | | | | | this option works very badly with caching and the API is likely not what we want either. Work continues for #8126 including that the additional work in I9f162e0a09c1ed327dd19498aac193f649333a01 tries to add new recursive features. This reverts commit b3a1162553879d1369154e920f3f4129020bb88e.
* new features for pep 593 AnnotatedMike Bayer2022-06-154-13/+57
| | | | | | | | | | | | | | | * extract the inner type from Annotated when the outer type isn't present in the type map, to allow for arbitrary Annotated * allow _IntrospectsAnnotations objects to be directly present in an Annotated and resolve the mapper property from that. Currently implemented for mapped_column(), with message for others. Can work for composite() and likely some relationship() as well at some point References: https://twitter.com/zzzeek/status/1536693554621341697 and replies Change-Id: I04657050a8785f194bf8f63291faf3475af88781
* typing adjustments for compositesMike Bayer2022-06-144-26/+73
| | | | | | | | | * if dataclass isn't used, columns have to be named * _CompositeClassProto is not useful as dataclasses have no methods / bases we can use, so composite is against Any * Adjust session.get() feature to work w/ dataclass composites Change-Id: Icc606cc76871c738dc794ea4555fca8a1ab0e0fd
* add auto_recurse option to selectinload, immediateloadMike Bayer2022-06-112-10/+93
| | | | | | | | | | | | | | Added very experimental feature to the :func:`_orm.selectinload` and :func:`_orm.immediateload` loader options called :paramref:`_orm.selectinload.auto_recurse` / :paramref:`_orm.immediateload.auto_recurse` , which when set to True will cause a self-referential relationship load to continue loading with arbitrary depth until no further objects are found. This may be useful for self-referential structures that must be loaded fully eagerly, such as when using asyncio. Fixes: #8126 Change-Id: I5bbd00bd0ca43f4649b44680fea1e84680f0a5db
* remove "undefer_pks" as a strategy optionMike Bayer2022-06-102-2/+0
| | | | | | | | | | | | | | The behavior of :func:`_orm.defer` regarding primary key and "polymorphic discriminator" columns is revised such that these columns are no longer deferrable, either explicitly or when using a wildcard such as ``defer('*')``. Previously, a wildcard deferral would not load PK/polymorphic columns which led to errors in all cases, as the ORM relies upon these columns to produce object identities. The behavior of explicit deferral of primary key columns is unchanged as these deferrals already were implicitly ignored. Fixes: #7495 Change-Id: I76d9252426e86619bc142667670a3df75b4f5f6a
* handle non-mapped mixins for with_loader_criteria reduceMike Bayer2022-06-091-1/+1
| | | | | | | | | special handling is needed for a with_loader_criteria() against a non-mapped mixin class. added that to test coverage Fixes: #8109 Change-Id: Ia599361c8faab008e92095eb4607d02820f590d5
* suppport with_loader_criteria pickling w/ fixed callableMike Bayer2022-06-082-4/+31
| | | | | | | | | | | | | Fixed issue where a :func:`_orm.with_loader_criteria` option could not be pickled, as is necessary when it is carried along for propagation to lazy loaders in conjunction with a caching scheme. Currently, the only form that is supported as picklable is to pass the "where criteria" as a fixed module-level callable function that produces a SQL expression. An ad-hoc "lambda" can't be pickled, and a SQL expression object is usually not fully picklable directly. Fixes: #8109 Change-Id: I49fe69088b0c7e58a0f22c67d2ea4e33752a5a73
* add note re: pickling for loader criteriaMike Bayer2022-06-081-1/+9
| | | | | Change-Id: I75992af71ba08799a03995178a6e4612c9a7428a References: #8108
* Merge "migrate labels to new tutorial" into mainmike bayer2022-06-073-13/+4
|\
| * migrate labels to new tutorialMike Bayer2022-06-073-13/+4
| | | | | | | | | | | | | | other org changes and some sections from old tutorial ported to new tutorial. Change-Id: Ic0fba60ec82fff481890887beef9ed0fa271875a
* | Merge "Generalize RETURNING and suppor for MariaDB / SQLite" into mainmike bayer2022-06-051-15/+30
|\ \ | |/ |/|
| * Generalize RETURNING and suppor for MariaDB / SQLiteDaniel Black2022-06-021-15/+30
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | As almost every dialect supports RETURNING now, RETURNING is also made more of a default assumption. * the default compiler generates a RETURNING clause now when specified; CompileError is no longer raised. * The dialect-level implicit_returning parameter now has no effect. It's not fully clear if there are real world cases relying on the dialect-level parameter, so we will see once 2.0 is released. ORM-level RETURNING can be disabled at the table level, and perhaps "implicit returning" should become an ORM-level option at some point as that's where it applies. * Altered ORM update() / delete() to respect table-level implicit returning for fetch. * Since MariaDB doesnt support UPDATE returning, "full_returning" is now split into insert_returning, update_returning, delete_returning * Crazy new thing. Dialects that have *both* cursor.lastrowid *and* returning. so now we can pick between them for SQLite and mariadb. so, we are trying to keep it on .lastrowid for simple inserts with an autoincrement column, this helps with some edge case test scenarios and i bet .lastrowid is faster anyway. any return_defaults() / multiparams etc then we use returning * SQLite decided they dont want to return rows that match in ON CONFLICT. this is flat out wrong, but for now we need to work with it. Fixes: #6195 Fixes: #7011 Closes: #7047 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/7047 Pull-request-sha: d25d5ea3abe094f282c53c7dd87f5f53a9e85248 Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com> Change-Id: I9908ce0ff7bdc50bd5b27722081767c31c19a950
* | Merge "Fixed orm not applying fetch" into mainmike bayer2022-06-041-0/+8
|\ \
| * | Fixed orm not applying fetchFederico Caselli2022-06-041-0/+8
| |/ | | | | | | | | | | | | | | Fixed an issue where :meth:`_sql.GenerativeSelect.fetch` would be ignored when executing a statement using the ORM. Fixes: #8091 Change-Id: I6790c7272a71278e90de2529c8bc8ae89e54e288
* | some typing fixesMike Bayer2022-06-033-17/+79
|/ | | | | | | | | | | | | | | | | | | | | | * ClassVar for decl fields, add __tablename__ * dataclasses require annotations for all fields. For us, if no annotation, then skip that field as part of what is considered to be a "dataclass", as this matches the behavior of pyright right now. We could alternatively raise on this use, which is what dataclasses does. we should ask the pep people * plain field that's just "str", "int", etc., with no value. Disallow it unless __allow_unmapped__ is set. If field has dataclasses.field, Column, None, a value etc, it goes through, and when using dataclasses mixin all such fields are considered for the dataclass setup just like a dataclass. Hopefully this does not have major backwards compat issues. __allow_unmapped__ can be set on the base class, mixins, etc., it's liberal for now in case people have this problem. * accommodate for ClassVar, these are not considered at all for mapping. Change-Id: Id743aa0456bade9a5d5832796caeecc3dc4accb7
* Merge "Support handle_error for pre_ping" into mainmike bayer2022-06-011-6/+6
|\
| * Support handle_error for pre_pingMike Bayer2022-05-311-6/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | The :meth:`.DialectEvents.handle_error` event is now moved to the :class:`.DialectEvents` suite from the :class:`.EngineEvents` suite, and now participates in the connection pool "pre ping" event for those dialects that make use of disconnect codes in order to detect if the database is live. This allows end-user code to alter the state of "pre ping". Note that this does not include dialects which contain a native "ping" method such as that of psycopg2 or most MySQL dialects. Fixes: #5648 Change-Id: I353d84a4f66f309d2467b7e67621db6b8c70411e
* | Merge "establish sessionmaker and async_sessionmaker as generic" into mainmike bayer2022-05-312-15/+37
|\ \ | |/ |/|
| * establish sessionmaker and async_sessionmaker as genericMike Bayer2022-05-312-15/+37
| | | | | | | | | | | | | | | | | | | | | | This is so that custom Session and AsyncSession classes can be typed for these factories. Added appropriate typevars to `__call__()`, `__enter__()` and other methods so that a custom Session or AsyncSession subclass is carried through. Fixes: #7656 Change-Id: Ia2b8c1f22b4410db26005c3285f6ba3d13d7f0e0
* | remove "deannotate" from column_property expressionMike Bayer2022-05-293-15/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Fixed issue where using a :func:`_orm.column_property` construct containing a subquery against an already-mapped column attribute would not correctly apply ORM-compilation behaviors to the subquery, including that the "IN" expression added for a single-table inherits expression would fail to be included. This fix involves a few tweaks in the ORM adaptation logic, including a missing "parententity" adaptation on the mapper side. The specific mechanics here have a lot of moving parts so we will continue to add tests to assert these cases. In particular a more complete test for issue #2316 is added that was relying upon the deannotate happening here. Fixes: #8064 Change-Id: Ia85dd12dcf6e7c002b30de4a27d7aa66cb3cd20e
* | QueryableAttribute can be used as DDL columnMike Bayer2022-05-261-0/+1
|/ | | | | | | We have a lot of mappings with like ForeignKey(A.id) so this needs to be included. Change-Id: I8ac4211fb09720f093fe7f6353f365ee1d7faaae
* Improvements on dataclass_transform featureFederico Caselli2022-05-222-20/+77
| | | | Change-Id: Iaf80526b70368cd4ed4147fdce9f6525b113474a
* implement dataclass_transformsMike Bayer2022-05-2010-157/+756
| | | | | | | | | | | | | | | | | | | | | | | | Implement a new means of creating a mapped dataclass where instead of applying the `@dataclass` decorator distinctly, the declarative process itself can create the dataclass. MapperProperty and MappedColumn objects themselves take the place of the dataclasses.Field object when constructing the class. The overall approach is made possible at the typing level using pep-681 dataclass transforms [1]. This new approach should be able to completely supersede the previous "dataclasses" approach of embedding metadata into Field() objects, which remains a mutually exclusive declarative setup style (mixing them introduces new issues that are not worth solving). [1] https://peps.python.org/pep-0681/#transform-descriptor-types-example Fixes: #7642 Change-Id: I6ba88a87c5df38270317b4faf085904d91c8a63c
* remove unused "descriptor" param from ColumnPropertyMike Bayer2022-05-173-7/+9
| | | | | | | | | | | this appears to be unused and wasn't documented, even though in the method sig for column_property(). just whack it also repairs a typing issue in decl_base that seemed to somehow not fail previously. Change-Id: Id5c8e716b7e53715778921ab03707642cadbfdee
* Merge "ensure MappedColumn col is mapped under alternate key" into mainmike bayer2022-05-161-7/+14
|\
| * ensure MappedColumn col is mapped under alternate keyMike Bayer2022-05-151-7/+14
| | | | | | | | | | Fixes: #8025 Change-Id: I83ba54f05bd2e5fc87d80f42fbeb6d4d2f2ac5fa
* | revenge of pep 484Mike Bayer2022-05-1530-1543/+3137
|/ | | | | | trying to get remaining must-haves for ORM Change-Id: I66a3ecbbb8e5ba37c818c8a92737b576ecf012f7
* update for flake8-future-imports 0.0.5Mike Bayer2022-05-148-31/+18
| | | | | | | | a whole bunch of errors were apparently blocked by 0.0.4 being installed. Fixes: #8020 Change-Id: I22a0faeaabe03de501897893391946d677c2df7e