[FIX] models: exists() should not consider record with id 0 as existing
This commit is contained in:
parent
4bcb31f706
commit
894a898e9e
|
@ -111,6 +111,18 @@ class TestORM(common.TransactionCase):
|
||||||
found = self.partner.search_read(self.cr, UID, [['name', '=', 'Does not exists']], ['name'])
|
found = self.partner.search_read(self.cr, UID, [['name', '=', 'Does not exists']], ['name'])
|
||||||
self.assertEqual(len(found), 0)
|
self.assertEqual(len(found), 0)
|
||||||
|
|
||||||
|
def test_exists(self):
|
||||||
|
partner = self.partner.browse(self.cr, UID, [])
|
||||||
|
|
||||||
|
# check that records obtained from search exist
|
||||||
|
recs = partner.search([])
|
||||||
|
self.assertTrue(recs)
|
||||||
|
self.assertEqual(recs.exists(), recs)
|
||||||
|
|
||||||
|
# check that there is no record with id 0
|
||||||
|
recs = partner.browse([0])
|
||||||
|
self.assertFalse(recs.exists())
|
||||||
|
|
||||||
def test_groupby_date(self):
|
def test_groupby_date(self):
|
||||||
partners = dict(
|
partners = dict(
|
||||||
A='2012-11-19',
|
A='2012-11-19',
|
||||||
|
|
|
@ -4774,14 +4774,15 @@ class BaseModel(object):
|
||||||
|
|
||||||
By convention, new records are returned as existing.
|
By convention, new records are returned as existing.
|
||||||
"""
|
"""
|
||||||
ids = filter(None, self._ids) # ids to check in database
|
ids, new_ids = [], []
|
||||||
|
for i in self._ids:
|
||||||
|
(ids if isinstance(i, (int, long)) else new_ids).append(i)
|
||||||
if not ids:
|
if not ids:
|
||||||
return self
|
return self
|
||||||
query = """SELECT id FROM "%s" WHERE id IN %%s""" % self._table
|
query = """SELECT id FROM "%s" WHERE id IN %%s""" % self._table
|
||||||
self._cr.execute(query, (ids,))
|
self._cr.execute(query, [tuple(ids)])
|
||||||
ids = ([r[0] for r in self._cr.fetchall()] + # ids in database
|
ids = [r[0] for r in self._cr.fetchall()]
|
||||||
[id for id in self._ids if not id]) # new ids
|
existing = self.browse(ids + new_ids)
|
||||||
existing = self.browse(ids)
|
|
||||||
if len(existing) < len(self):
|
if len(existing) < len(self):
|
||||||
# mark missing records in cache with a failed value
|
# mark missing records in cache with a failed value
|
||||||
exc = MissingError(_("Record does not exist or has been deleted."))
|
exc = MissingError(_("Record does not exist or has been deleted."))
|
||||||
|
|
Loading…
Reference in New Issue