postgresql - 如何在 Odoo 8 中创建 One2one 关系?

标签 postgresql odoo odoo-8 postgresql-9.4

我有两组呈现一对一关系的数据。

我无法合并这两组数据,因为:

  1. 特定记录可能仅存在于集合 A 中,仅存在于集合 B 中,或同时存在于集合 A 和集合 B 中;和
  2. 集合 A 和集合 B 中的记录之间的关联是暂时的,这意味着记录可以关联也可以解除关联;和
  3. A 组数据的处理方式与 B 组数据不同;和
  4. 存在外部架构限制。

当集合 A 中的一条记录与集合 B 中的一条记录相关联时,我想链接这两条记录。链接记录时,关系必须是一对一的。 如何保证关系是一对一的关系?

下面的代码看起来很接近,但我是 Odoo 的新手,不确定如何分析这种方法是否保证一对一关系。

import openerp

class A(openerp.models.Model):

  _name = 'set.a'

  _sql_constraints = [
    ('set_b_id', 'unique("set_b_id")', 'Field set_b_id must be unique.'),
  ]

  # Constrained to be unique (see SQL above) which essentially changes
  # this end of the Many2one relationship to a One2one relationship. (The
  # other end of the relationship must also be constrained.)

  set_b_id = openerp.fields.Many2one(
    comodel_name='set.b',
  )

class B(openerp.models.Model):

  _name = 'set.b'

  # Constrained to tie with either zero keys or one key (see function
  # below) which essentially changes this end of the One2many
  # relationship to a One2one relationship. (The other end of the
  # relationship must also be constrained.)

  set_a_id = openerp.fields.One2many(
    comodel_name='set.a',
    inverse_name='set_b_id',
  )

  @openerp.api.constrains('set_a_id')
  def _constrains_set_a_id(self):
    if len(self.set_a_id) > 1:
        raise openerp.exceptions.ValidationError('Additional linkage failed.')

另一种方法可能是扩展 openerp.fields 以重新创建以前弃用的 One2one 关系,但我不确定是否可以干净地完成。

最佳答案

在您的情况下,基本上一对一关系在 Odoo 8.0 中不可用,它在 Odoo(正式的 OpenERP)中完全弃用了一个 7.0 或更高版本。

所以我的建议是不要使用一对一关系,只需将其用作 many2one 并根据需要设置本地。

希望我的回答对你有帮助:)

关于postgresql - 如何在 Odoo 8 中创建 One2one 关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32801526/

相关文章:

sql - 子查询返回多列 - 或近似值

postgresql - Google Cloud SQL - Postgresql 存储不断增长

sql - 使用 SQL 定义一对多关系

python - odoo 9 获取当前记录中的字段值

javascript - 如何在传递给小部件的其他字段的 Js 中获取值?奥多

css - 如何使用 Odoo 8 中的下拉菜单将左侧菜单移动到页面顶部?

mysql - Mysql中Postgresql regexp_split_to_table有什么解决办法

nginx - Odoo(在 Debian 上)- longpolling 端口从未使用/打开

python - 用户在自定义模块中编辑 many2one 字段时出现 AccessError

python - 值错误 : "undefined get method !" while evaluating u'action_ship_create()' in Odoo