Rails 自动分配已存在的 id [英] Rails auto-assigning id that already exists

查看:15
本文介绍了Rails 自动分配已存在的 id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我像这样创建一个新记录:

I create a new record like so:

truck = Truck.create(:name=>name, :user_id=>2)

我的数据库目前有数千个卡车实体,但我将 ID 分配给了其中的几个,从而使一些 ID 可用.所以发生的事情是 rails 创建了 id = 150 的项目并且它工作正常.但随后它尝试创建一个项目并为其分配 id = 151,但该 id 可能已经存在,所以我看到了这个错误:

My database currently has several thousand entities for truck, but I assigned the id's to several of them, in a way that left some id's available. So what's happening is rails creates item with id = 150 and it works fine. But then it tries to create an item and assign it id = 151, but that id may already exist, so I'm seeing this error:

ActiveRecord::RecordNotUnique (PG::Error: ERROR: 重复键值违反唯一约束companies_pkey"详细信息:密钥 (id)=(151) 已经存在.

下次我运行该操作时,它会简单地分配 id 152,如果尚未采用该值,这将正常工作.如何让 Rails 在分配 ID 之前检查它是否已经存在?

And the next time I run the action, it will simply assign the id 152, which will work fine if that value isn't already taken. How can I get rails to check whether an ID already exists before it assigns it?

谢谢!

编辑

卡车 ID 是被复制的.用户已经存在并且在这种情况下是一个常量.这实际上是我必须处理的遗留问题.一种选择是重新创建表,让 Rails 这次自动分配每个 id.我开始认为这可能是最好的选择,因为我还有其他一些问题,但是执行此操作的迁移将非常复杂,因为 Truck 是许多其他表中的外键.是否有一种简单的方法可以让 rails 创建一个新表,其中包含已存储在 Truck 下的相同数据、自动分配的 ID 并维护所有现有关系?

The Truck id is what is being duplicated. The user already exists and is a constant in this case. It actually is a legacy issue that I have to deal with. One option, is to re-create the table at let rails auto assign every id this time around. I'm beginning to think this may be the best choice because I'm have a few other problems, but the migration for doing this would be very complicated because Truck is a foreign key in so many other tables. Would there be a simple way to have rails create a new table with the same data already stored under Truck, with auto-assigned ID's and maintaining all existing relationships?

推荐答案

Rails 可能正在使用内置的 PostgreSQL 序列.序列的想法是它只使用一次.

Rails is probably using the built-in PostgreSQL sequence. The idea of a sequence is that it is only used once.

最简单的解决方案是使用如下查询将 company.id 列的序列设置为表中的最大值:

The simplest solution is to set the sequence for your company.id column to the highest value in the table with a query like this:

SELECT setval('company_id_seq', (SELECT max(id) FROM company));

我在猜测您的序列名称company_id_seq"、表名称company"和列名称id"……请将它们替换为正确的.您可以使用 SELECT pg_get_serial_sequence('tablename', 'columname'); 获取序列名称,或者使用 d tablename 查看表定义.

I am guessing at your sequence name "company_id_seq", table name "company", and column name "id" ... please replace them with the correct ones. You can get the sequence name with SELECT pg_get_serial_sequence('tablename', 'columname'); or look at the table definition with d tablename.

另一种解决方案是覆盖公司类中的 save() 方法,以便在保存之前手动设置新行的公司 ID.

An alternate solution is to override the save() method in your company class to manually set the company id for new rows before saving.

这篇关于Rails 自动分配已存在的 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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