--- /dev/null
+.. change::
+ :tags: bug, orm
+ :tickets: 4915
+
+ 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.
+
def test_multiple_entity_false(self):
User = self.classes.User
- query = (
- create_session()
- .query(User.id, User)
- .only_return_tuples(False)
- )
+ query = create_session().query(User.id, User).only_return_tuples(False)
is_false(query.is_single_entity)
row = query.first()
assert isinstance(row, collections_abc.Sequence)
def test_multiple_entity_true(self):
User = self.classes.User
- query = (
- create_session()
- .query(User.id, User)
- .only_return_tuples(True)
- )
+ query = create_session().query(User.id, User).only_return_tuples(True)
is_false(query.is_single_entity)
row = query.first()
assert isinstance(row, collections_abc.Sequence)
q = s.query(User.id)
assert_raises(sa_exc.InvalidRequestError, q.get, (5,))
- def test_get_null_pk(self):
- """test that a mapping which can have None in a
- PK (i.e. map to an outerjoin) works with get()."""
-
+ @testing.fixture
+ def outerjoin_mapping(self):
users, addresses = self.tables.users, self.tables.addresses
s = users.outerjoin(addresses)
"address_id": addresses.c.id,
},
)
+ return UserThing
+
+ def test_get_null_pk(self, outerjoin_mapping):
+ """test that a mapping which can have None in a
+ PK (i.e. map to an outerjoin) works with get()."""
+
+ UserThing = outerjoin_mapping
sess = create_session()
u10 = sess.query(UserThing).get((10, None))
eq_(u10, UserThing(id=10))
+ def test_get_fully_null_pk(self):
+ User = self.classes.User
+
+ s = Session()
+ q = s.query(User)
+ assert_raises_message(
+ sa_exc.SAWarning,
+ r"fully NULL primary key identity cannot load any object. "
+ "This condition may raise an error in a future release.",
+ q.get,
+ None,
+ )
+
+ def test_get_fully_null_composite_pk(self, outerjoin_mapping):
+ UserThing = outerjoin_mapping
+
+ s = Session()
+ q = s.query(UserThing)
+
+ assert_raises_message(
+ sa_exc.SAWarning,
+ r"fully NULL primary key identity cannot load any object. "
+ "This condition may raise an error in a future release.",
+ q.get,
+ (None, None),
+ )
+
def test_no_criterion(self):
"""test that get()/load() does not use preexisting filter/etc.
criterion"""