diff --git a/trytond/model/modelsql.py b/trytond/model/modelsql.py index 246e187493..2f07e807c7 100644 --- a/trytond/model/modelsql.py +++ b/trytond/model/modelsql.py @@ -1136,7 +1136,7 @@ class ModelSQL(ModelStorage): table = convert_from(None, tables) if count: - cursor.execute(*table.select(Count(Literal(1)), + cursor.execute(*table.select(Count(Literal('*')), where=expression, limit=limit, offset=offset)) return cursor.fetchone()[0] # execute the "main" query to fetch the ids we were searching for diff --git a/trytond/model/modelstorage.py b/trytond/model/modelstorage.py index aa25c0f37c..9a5fd5d489 100644 --- a/trytond/model/modelstorage.py +++ b/trytond/model/modelstorage.py @@ -366,7 +366,7 @@ class ModelStorage(Model): ''' Return the number of records that match the domain. ''' - res = cls.search(domain, count=True) + res = cls.search(domain, order=[], count=True) if isinstance(res, list): return len(res) return res diff --git a/trytond/tests/test_modelstorage.py b/trytond/tests/test_modelstorage.py index 72868ec753..88d979b37c 100644 --- a/trytond/tests/test_modelstorage.py +++ b/trytond/tests/test_modelstorage.py @@ -36,6 +36,19 @@ class ModelStorageTestCase(unittest.TestCase): self.assertTrue( all(x['name'] >= y['name'] for x, y in zip(rows, rows[1:]))) + @with_transaction() + def test_search_count(self): + "Test search_count" + pool = Pool() + ModelStorage = pool.get('test.modelstorage') + ModelStorage.create([{'name': 'Test %s' % i} for i in range(10)]) + + count = ModelStorage.search_count([]) + self.assertEqual(count, 10) + + count = ModelStorage.search_count([('name', '=', 'Test 5')]) + self.assertEqual(count, 1) + @with_transaction() def test_browse_context(self): 'Test context when browsing'