diff options
| author | lizraeli <leo2002b@yahoo.com> | 2019-10-28 15:33:41 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-10-28 16:04:29 -0400 |
| commit | 997f4b5f2b3b4725de0960824e95fcb1150ff215 (patch) | |
| tree | 1fb16c9d6c479d72a7a34c9f721678a1bbf32d5d /lib/sqlalchemy | |
| parent | ec5e4f3543cc2896c83c5854cc5c39a80cc11b2f (diff) | |
| download | sqlalchemy-997f4b5f2b3b4725de0960824e95fcb1150ff215.tar.gz | |
Correctly interpret None passed to query.get(); warn for empty PK values
A warning is emitted if a primary key value is passed to :meth:`.Query.get`
that consists of None for all primary key column positions. Previously,
passing a single None outside of a tuple would raise a ``TypeError`` and
passing a composite None (tuple of None values) would silently pass
through. The fix now coerces the single None into a tuple where it is
handled consistently with the other None conditions. Thanks to Lev
Izraelit for the help with this.
Fixes #4915
Closes: #4917
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/4917
Pull-request-sha: b388343c7cabeecf8c779689b78e638c23f9af40
Change-Id: Ibc6c27ccf50dfd4adbf15b6dbd393115c30d44fb
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/orm/loading.py | 6 | ||||
| -rw-r--r-- | lib/sqlalchemy/orm/query.py | 4 |
2 files changed, 9 insertions, 1 deletions
diff --git a/lib/sqlalchemy/orm/loading.py b/lib/sqlalchemy/orm/loading.py index 25ba8a398..189da78bc 100644 --- a/lib/sqlalchemy/orm/loading.py +++ b/lib/sqlalchemy/orm/loading.py @@ -243,6 +243,12 @@ def load_on_pk_identity( ) _get_clause = sql_util.adapt_criterion_to_null(_get_clause, nones) + if len(nones) == len(primary_key_identity): + util.warn( + "fully NULL primary key identity cannot load any " + "object. This condition may raise an error in a future " + "release." + ) _get_clause = q._adapt_clause(_get_clause, True, False) q._criterion = _get_clause diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index d515fa83a..80d544068 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -977,7 +977,9 @@ class Query(Generative): is_dict = isinstance(primary_key_identity, dict) if not is_dict: - primary_key_identity = util.to_list(primary_key_identity) + primary_key_identity = util.to_list( + primary_key_identity, default=(None,) + ) if len(primary_key_identity) != len(mapper.primary_key): raise sa_exc.InvalidRequestError( |
