Rails:Oracle约束违反 [英] Rails: Oracle constraint violation

查看:161
本文介绍了Rails:Oracle约束违反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我继承的Rails网站上进行维护工作;它由Oracle数据库驱动,我可以访问该站点的开发和生产安装(每个都有自己的Oracle DB)。我试图在生产网站,但不是开发网站插入数据时遇到Oracle错误:

  ActiveRecord: :StatementInvalid(OCIError:ORA-00001:unique constraint(DATABASE_NAME.PK_REGISTRATION_OWNERSHIP)violated:INSERT INTO registration_ownerships(updated_at,company_ownership_id,created_by,updated_by,registration_id,created_at)VALUES('2006-05-04 16:30:47',3 ,NULL,NULL,2920,'2006-05-04 16:30:47')):
/usr/local/lib/ruby/gems/1.8/gems/activerecord-oracle-adapter-1.0.0.9250 /lib/active_record/connection_adapters/oracle_adapter.rb:221:in`execute'
app / controllers / vendors_controller.rb:94:在`create'中

据我所知(我使用Navicat作为Oracle客户端),dev网站的数据库模式与活动网站的数据库模式相同。我不是Oracle专家;任何人都可以了解为什么我会在一个安装而不是其他的错误?



顺便说一下,开发和生产registration_ownerships表填充大量数据,包括country_ownership_id(由索引PK_REGISTRATION_OWNERSHIP驱动)的重复条目。如果您需要更多信息进行问题排查,请告诉我们。对不起,我没有提供更多,但我只是不知道哪些细节会有所帮助。



UPDATE :I试图放弃对生产服务器的约束,但它没有效果;我不想丢弃索引,因为我不知道什么后果可能是,我不想让生产不稳定比它已经是。奇怪的是,我试图手工执行的SQL抛出一个错误,并且Oracle接受insert语句(虽然我不得不将日期包装在to_date()调用中字符串字面值来解决ORA-01861:字面量不匹配格式字符串错误)。这里可能发生什么?

解决方案

发生这种情况时,在目录周围有一个注册即使它有一个不同的名称(registrations_2349871.rb或类似的东西)Rails正在运行所有模型功能(保存,验证等)两次,因此关键约束违反!我从来没有见过这样的行为。删除流氓文件解决了这个问题。


I'm doing maintenance work on a Rails site that I inherited; it's driven by an Oracle database, and I've got access to both development and production installations of the site (each with its own Oracle DB). I'm running into an Oracle error when trying to insert data on the production site, but not the dev site:

ActiveRecord::StatementInvalid (OCIError: ORA-00001: unique constraint (DATABASE_NAME.PK_REGISTRATION_OWNERSHIP) violated: INSERT INTO registration_ownerships (updated_at, company_ownership_id, created_by, updated_by, registration_id, created_at) VALUES ('2006-05-04 16:30:47', 3, NULL, NULL, 2920, '2006-05-04 16:30:47')):
/usr/local/lib/ruby/gems/1.8/gems/activerecord-oracle-adapter-1.0.0.9250/lib/active_record/connection_adapters/oracle_adapter.rb:221:in `execute'
app/controllers/vendors_controller.rb:94:in `create'

As far as I can tell (I'm using Navicat as an Oracle client), the DB schema for the dev site is identical to that of the live site. I'm not an Oracle expert; can anyone shed light on why I'd be getting the error in one installation and not the other?

Incidentally, both dev and production registration_ownerships tables are populated with lots of data, including duplicate entries for country_ownership_id (driven by index PK_REGISTRATION_OWNERSHIP). Please let me know if you need more information to troubleshoot. I'm sorry I haven't given more already, but I just wasn't sure which details would be helpful.

UPDATE: I've tried dropping the constraint on the production server but it had no effect; I didn't want to drop the index as well because I'm not sure what the consequences might be and I don't want to make production less stable than it already is.

Curiously, I tried executing by hand the SQL that was throwing an error, and Oracle accepted the insert statement (though I had to wrap the dates in to_date() calls with string literals to get around an "ORA-01861: literal does not match format string" error). What might be going on here?

解决方案

As it happens, there was a spare copy of the "registrations" model lying around the directory; even though it had a different name ("registrations_2349871.rb" or something like that) Rails was running all model functionality (saving, validating, etc) twice, hence the key constraint violation! I've never seen behavior like this before. Deleting the rogue file solved the problem.

这篇关于Rails:Oracle约束违反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆