44
44
45
45
from .exceptions import MigrationError , SleepyDeveloperError
46
46
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
48
48
49
49
_logger = logging .getLogger (__name__ )
50
50
@@ -1339,14 +1339,34 @@ def drop_depending_views(cr, table, column):
1339
1339
1340
1340
1341
1341
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
+ """
1342
1359
if col1 is None :
1343
1360
col1 = "%s_id" % fk1
1344
1361
if col2 is None :
1345
1362
col2 = "%s_id" % fk2
1346
1363
1364
+ if m2m is AUTO :
1365
+ m2m = "{}_{}_rel" .format (* sorted ([fk1 , fk2 ]))
1366
+
1347
1367
if table_exists (cr , m2m ):
1348
1368
fixup_m2m (cr , m2m , fk1 , fk2 , col1 , col2 )
1349
- return
1369
+ return m2m
1350
1370
1351
1371
query = format_query (
1352
1372
cr ,
@@ -1366,6 +1386,8 @@ def create_m2m(cr, m2m, fk1, fk2, col1=None, col2=None):
1366
1386
)
1367
1387
cr .execute (query )
1368
1388
1389
+ return m2m
1390
+
1369
1391
1370
1392
def update_m2m_tables (cr , old_table , new_table , ignored_m2ms = ()):
1371
1393
"""
0 commit comments