由此:如何使主键从1000开始?< /a> 看来我需要发出 SQL 命令: ALTER TABLE tbl AUTO_INCRMENT = 1000;
但我只是通过 ActiveRecord 抽象来处理数据库。有没有办法通过活动记录来实现它?是在迁移时还是在数据库上创建新记录时即时进行?
我已经尝试了以下两种方法,但都失败了:
@record= myRecord.new
while @record.id < 1000 do
@record= myRecord.new
end
它效率低下,但只会发生一次,但 Rails 报告 @record.id 为零,因此无法执行 <<比较
所以我尝试先保存@record,然后查看数据库分配给它的id(主键)值
@record.save
if @record.id <1000
@record.id = 1000 + @record.id
@record.save!
end
不知何故,rails报告说@record中的唯一字段之一已经存在,因此无法再次保存@record 。
编辑:我的错,上面是 MySQL 命令...对于 PostgreSQL,它似乎是沿线的东西( http://archives.postgresql.org/pgsql-general/2006-10/msg01417.php ):
SELECT setval('id_seq',100111);
然而,我尝试在我的部署环境(Heroku 上的共享数据库)的 Ruby 控制台上运行它,但我刚刚得到了!内部服务器错误:-(
我的 ruby 类被称为:
class Mytable < ActiveRecord::Base
所以我运行此命令:
Mytable.connection.execute('SELECT setval('id_seq', 1000)')
并收到内部服务器错误(在上述命令中尝试使用“id”和“id_seq”)
但可能是 Heroku 上的某种 Ruby 特定问题造成了麻烦,所以我会调查并发布另一个问题,而不是更改这个问题。谢谢!
加法相关的PostgreSQL命令资料:
http://pointbeing.net/weblog/2008/03/mysql-versus-postgresql-adding-an-auto-increment-column-to-a-table.html
http://archives.postgresql.org/pgsql-general/2006-10/msg01417.php
当 postgres 不同步时如何重置主键序列?
From this: How to make a primary key start from 1000? It seems that I need to issue the SQL command: ALTER TABLE tbl AUTO_INCREMENT = 1000;
But I only have been dealing with the database through the ActiveRecord abstraction. Is there a way to achieve it via active record? Either at the time of migration or on the fly when creating new record on the database?
I have tried both the following and failed:
@record= myRecord.new
while @record.id < 1000 do
@record= myRecord.new
end
It is inefficient but it would just happen once, yet Rails report that @record.id is nil so cannot do the < comparasion
and so I try to save the @record first and then see what id (primary key) value it has been assigned by the database
@record.save
if @record.id <1000
@record.id = 1000 + @record.id
@record.save!
end
Somehow rails reports back that one of the unique field in @record is already there so cannot save the @record again.
EDIT: My bad, the above is MySQL command... for PostgreSQL, it seems to be something along the line ( http://archives.postgresql.org/pgsql-general/2006-10/msg01417.php ):
SELECT setval('id_seq',100111);
However, I tried to run it on the Ruby console of my deployment environment (shared database on Heroku) and I just got !Internal server error back :-(
my ruby class is called:
class Mytable < ActiveRecord::Base
so I run this command:
Mytable.connection.execute('SELECT setval('id_seq', 1000)')
and got Internal server error (tried with both 'id' and 'id_seq' in the above command)
But it may be some sort of Ruby on Heroku specific issue that is causing the trouble, so I would investigate and posts another question instead of changing this one. Thanks!
Addition related PostgreSQL command materials:
http://pointbeing.net/weblog/2008/03/mysql-versus-postgresql-adding-an-auto-increment-column-to-a-table.html
http://archives.postgresql.org/pgsql-general/2006-10/msg01417.php
How to reset postgres' primary key sequence when it falls out of sync?
发布评论
评论(1)
您可以像这样执行原始 sql:
或者从 ActiveRecord::Base 的任何后代(即任何模型类)执行原始 sql,如下所示:
You can execute raw sql like this:
Or from any descendant of ActiveRecord::Base (i.e. any model class), like this: