Skip to content

Commit 4a9fdd8

Browse files
committed
[IMP] create_m2m: auto generate and return table name
In order to limit chances for typos, while keeping the util backwards compatible, allow users to provide the `util.AUTOMATIC` sentinel value to automatically generate the m2m table name, following the same rule as the ORM[^1]. The function now return the m2m table name. [^1]: Example from 18.0 https://github.com/odoo/odoo/blob/f42fe80576c8b419b5215ff17859e0dfebcd996a/odoo/fields.py#L4918-L4923 Part-of: #304 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 56a18c1 commit 4a9fdd8

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/base/tests/test_util.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,18 @@ def test_ColumnList(self):
10451045
self.assertTrue(s(ulc.using(alias="x")), ', "x"."a", "x"."A"')
10461046
self.assertIs(ulc, ulc.using(leading_comma=True))
10471047

1048+
def test_create_m2m(self):
1049+
cr = self.env.cr
1050+
1051+
m2m_name = "random_table_name"
1052+
created_m2m = util.create_m2m(cr, m2m_name, "res_users", "res_groups")
1053+
self.assertEqual(m2m_name, created_m2m)
1054+
self.assertTrue(util.table_exists(cr, created_m2m))
1055+
1056+
auto_generated_m2m_table_name = util.create_m2m(cr, util.AUTO, "res_users", "res_groups")
1057+
self.assertEqual("res_groups_res_users_rel", auto_generated_m2m_table_name)
1058+
self.assertTrue(util.table_exists(cr, auto_generated_m2m_table_name))
1059+
10481060

10491061
class TestORM(UnitTestCase):
10501062
def test_create_cron(self):

src/util/pg.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
from .exceptions import MigrationError, SleepyDeveloperError
4646
from .helpers import _validate_table, model_of_table
47-
from .misc import Sentinel, log_progress, on_CI, version_gte
47+
from .misc import AUTO, Sentinel, log_progress, on_CI, version_gte
4848

4949
_logger = logging.getLogger(__name__)
5050

@@ -1339,14 +1339,34 @@ def drop_depending_views(cr, table, column):
13391339

13401340

13411341
def create_m2m(cr, m2m, fk1, fk2, col1=None, col2=None):
1342+
"""
1343+
Ensure a m2m table exists or is created.
1344+
1345+
This function creates the table associated to a m2m field.
1346+
If the table already exists, :func:`~odoo.upgrade.util.pg.fixup_m2m` is run on it.
1347+
The table name can be generated automatically, applying the same logic of the ORM.
1348+
In order to do so, use the value "auto" for the `m2m` parameter.
1349+
1350+
:param str m2m: table name to create,
1351+
if :const:`~odoo.upgrade.util.misc.AUTO` it is auto-generated
1352+
:param str fk1: first foreign key table name
1353+
:param str fk2: second foreign key table name
1354+
:param str col1: column referencing `fk1`, defaults to `"{fk1}_id"`
1355+
:param str col2: column referencing `fk2`, defaults to `"{fk2}_id"`
1356+
:return: the name of the table just created/fixed-up
1357+
:rtype: str
1358+
"""
13421359
if col1 is None:
13431360
col1 = "%s_id" % fk1
13441361
if col2 is None:
13451362
col2 = "%s_id" % fk2
13461363

1364+
if m2m is AUTO:
1365+
m2m = "{}_{}_rel".format(*sorted([fk1, fk2]))
1366+
13471367
if table_exists(cr, m2m):
13481368
fixup_m2m(cr, m2m, fk1, fk2, col1, col2)
1349-
return
1369+
return m2m
13501370

13511371
query = format_query(
13521372
cr,
@@ -1366,6 +1386,8 @@ def create_m2m(cr, m2m, fk1, fk2, col1=None, col2=None):
13661386
)
13671387
cr.execute(query)
13681388

1389+
return m2m
1390+
13691391

13701392
def update_m2m_tables(cr, old_table, new_table, ignored_m2ms=()):
13711393
"""

0 commit comments

Comments
 (0)