Many2many 向前引用问题
-
[code]# 驾驶员信息
class driver(osv.osv):
_name = "transit.driver"
_description = "驾驶员信息"
_inherits = {'hr.employee': 'name'}
_columns = {
'name': fields.many2one ('hr.employee','姓名', domain=[('is_driver', '=', "1")], required=True, help="驾驶员的姓名"),
'active': fields.boolean('有效'),
'code': fields.char ('驾照', size=128, help="驾驶证编号"),
'license': fields.char ('准驾证', size=128, help="准驾证编号"),
'vehicle_ids': fields.many2many ('transit.vehicle', 'transit_vehicles_drivers_rel','driver_id','vehicle_id', '相关车辆'), #应该就是这里引起的
'notes': fields.text ('备注'),
}
_defaults = {
'active': lambda *a: True,
}
_sql_constraints = [
('code_key', 'UNIQUE (code)', '驾照不能重复!'),
('license_key', 'UNIQUE (license)', '准驾证不能重复!')
]
driver()class vehicle(osv.osv):
_name = 'transit.vehicle'
_description = '车辆档案'def _get_models(self, cr, uid, context={}): return common.get_objects_for_selection(self, cr, uid, 'transit.vehicle.model', context) _columns = { 'name': fields.char('编号', size=50, required=True, help='公司或车队内部的自编号'), 'active': fields.boolean('有效'), 'license': fields.char('牌照', size=50, required=True), 'passenger_capacity': fields.integer('乘员数'), 'transit_capacity': fields.float('载重量(顿)'), 'description': fields.text('说明'), 'category_id': fields.many2one('transit.vehicle.category', '分类', required=True), 'model': fields.selection(_get_models, "车型"), 'driver_ids': fields.many2many ('transit.driver', 'transit_vehicles_drivers_rel','vehicle_id','driver_id', '驾驶员'), 'ein': fields.char('发动机号', size=128, required=False), 'insurance': fields.char('保险号', size=128, required=False), 'frame_number': fields.char('车架号', size=128, required=False), 'chassis_number': fields.char('底盘号', size=128, required=False), 'depreciation': fields.float('月折旧', digits=(16, int(config['price_accuracy'])), help='车辆月折旧费用的估计值'), 'oil_consumption': fields.float('百公里油耗', help='车辆百公里耗油量的估计值'), 'state': fields.selection([ ('ready','待命'), ('transporting','运输途中'), ('maintaining','正在维护'), ('scrapped','报废'), ], '车辆状态', readonly=True, help='该车辆在系统中的状态', select=True), } _defaults = { 'active': lambda *a: True, } _sql_constraints = [ ('license_key', 'UNIQUE (license)', '车辆牌照不能重复!') ]
vehicle()[/code]
-
详细错误信息:[code]
Traceback (most recent call last):
File "D:devopenerpstableserverbinnetsvc.py", line 235, in dispatch
result = LocalService(service_name)(method, *params)
File "D:devopenerpstableserverbinnetsvc.py", line 74, in call
return getattr(self, method)(*params)
File "D:devopenerpstableserverbinserviceweb_services.py", line 582, in execute
return self.execute(db, uid, wiz_id, datas, action, context)
File "D:devopenerpstableserverbinserviceweb_services.py", line 562, in execute
return wiz.execute(db, uid, self.wiz_datas[wiz_id], action, context)
File "D:devopenerpstableserverbinwizard__init.py", line 178, in execute
res = self.execute_cr(cr, uid, data, state, context)
File "D:devopenerpstableserverbinwizard__init__.py", line 74, in execute_cr
action_res = action(self, cr, uid, data, context)
File "D:devopenerpstableserverbinaddonsbasemodulewizardwizard_module_upgrade.py", line 92, in upgrade_module
db, pool = pooler.restart_pool(cr.dbname, update_module=True)
File "D:devopenerpstableserverbinpooler.py", line 62, in restart_pool
return get_db_and_pool(db_name, force_demo, status, update_module=update_module)
File "D:devopenerpstableserverbinpooler.py", line 40, in get_db_and_pool
addons.load_modules(db, force_demo, status, update_module)
File "D:devopenerpstableserverbinaddons__init_.py", line 718, in load_modules
r = load_module_graph(cr, graph, status, report=report)
File "D:devopenerpstableserverbinaddons__init__.py", line 581, in load_module_graph
init_module_objects(cr, package.name, modules)
File "D:devopenerpstableserverbinaddons__init__.py", line 366, in init_module_objects
result = obj._auto_init(cr, {'module': module_name})
File "D:devopenerpstableserverbinosvorm.py", line 1532, in _auto_init
cr.execute('CREATE TABLE "%s" ("%s" INTEGER NOT NULL REFERENCES "%s" ON DELETE CASCADE, "%s" INTEGER NOT NULL REFERENCES "%s" ON DELETE CASCADE) WITH OIDS' % (f._rel, f._id1, self._table, f._id2, ref))
File "D:devopenerpstableserverbinsql_db.py", line 76, in wrapper
return f(self, *args, **kwargs)
File "D:devopenerpstableserverbinsql_db.py", line 120, in execute
res = self._obj.execute(query, params)
ProgrammingError: relation "transit_vehicle" does not exist[/code]
-
这个错误应该是因为transit.vehicle对象还没有被初始化就被调用了。把vehicle类放到driver类之前试试?
另外,_inherits在这里用来表示一对一关系的,可是name,active字段与父类重复,应该也会有问题的。
-
哦,想简单了。
假设driver类在前,vehicle类在后。 在driver类中先不声明对vehicle的many2many引用,而在vehicle类之后再创建对driver的继承类并添加many2many field. 看看这样是不是可以? -
res.user和res.roles对应的表是在服务器初始化的时候由base.sql 创建好的,所以没有初始化的先后关系的问题。
-
oldrev, 你的代码现在调试通过了吗?我想跟踪一下,我也在官方论坛里问一下有没有更好的办法。