已存在的Rails自动分配ID [英] Rails auto-assigning id that already exists
问题描述
我这样创建新记录:
truck = Truck.create(:name=>name, :user_id=>2)
我的数据库当前有数千个卡车实体,但是我以一些可用的方式将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: duplicate key value violates unique constraint "companies_pkey"
DETAIL: Key (id)=(151) already exists.
ActiveRecord::RecordNotUnique (PG::Error: ERROR: duplicate key value violates unique constraint "companies_pkey"
DETAIL: Key (id)=(151) already exists.
下次我执行该操作时,它将简单地分配ID 152,如果尚未使用该值,它将很好地工作.如何在分配ID之前让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屋!