[IMP] orm: option to make many2many relation tables without foreign key constaint.

The option is not completely flexible (no possibility to choose to have a foreign
key on one of the fields. No similar option is provided for many2one either.
It is seldom used (e.g. in account_followup addons).

bzr revid: vmt@openerp.com-20110829094354-0yf6t6329j8e765q
This commit is contained in:
Vo Minh Thu 2011-08-29 11:43:54 +02:00
parent e10676bf66
commit e1b2dec200
2 changed files with 11 additions and 3 deletions

View File

@ -495,7 +495,13 @@ class many2many(_column):
_classic_write = False
_prefetch = False
_type = 'many2many'
def __init__(self, obj, rel, id1, id2, string='unknown', limit=None, **args):
def __init__(self, obj, rel, id1, id2, string='unknown', limit=None, no_foreign_keys=False, **args):
"""
:param no_foreign_keys: specifies if the relation table should not have
foreign keys constaints on ``id1`` and ``id2``. This option is
useful for many2many relationships involving views instead of
regular tables.
"""
_column.__init__(self, string=string, **args)
self._obj = obj
if '.' in rel:
@ -505,6 +511,7 @@ class many2many(_column):
self._id1 = id1
self._id2 = id2
self._limit = limit
self._no_foreign_keys = no_foreign_keys
def get(self, cr, obj, ids, name, user=None, offset=0, context=None, values=None):
if not context:

View File

@ -2855,8 +2855,9 @@ class Model(object):
raise except_orm('Programming Error', ('There is no reference available for %s') % (f._obj,))
ref = self.pool.get(f._obj)._table
cr.execute('CREATE TABLE "%s" ("%s" INTEGER NOT NULL, "%s" INTEGER NOT NULL, UNIQUE("%s","%s")) WITH OIDS' % (f._rel, f._id1, f._id2, f._id1, f._id2))
self._foreign_keys.append((f._rel, f._id1, self._table, 'CASCADE'))
self._foreign_keys.append((f._rel, f._id2, ref, 'CASCADE'))
if not f._no_foreign_keys:
self._foreign_keys.append((f._rel, f._id1, self._table, 'CASCADE'))
self._foreign_keys.append((f._rel, f._id2, ref, 'CASCADE'))
cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (f._rel, f._id1, f._rel, f._id1))
cr.execute('CREATE INDEX "%s_%s_index" ON "%s" ("%s")' % (f._rel, f._id2, f._rel, f._id2))
cr.execute("COMMENT ON TABLE \"%s\" IS 'RELATION BETWEEN %s AND %s'" % (f._rel, self._table, ref))