如何使用 PostgreSQL 强制 ActiveRecord 中的主键从特定整数值开始跳转?

发布于 2024-12-06 08:11:52 字数 1975 浏览 0 评论 0 原文

由此:如何使主键从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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

林空鹿饮溪 2024-12-13 08:11:52

您可以像这样执行原始 sql:

ActiveRecord::Base.connection.execute('ALTER TABLE tbl AUTO_INCREMENT = 1000');

或者从 ActiveRecord::Base 的任何后代(即任何模型类)执行原始 sql,如下所示:

MyRecord.connection.execute('ALTER TABLE tbl AUTO_INCREMENT = 1000')

You can execute raw sql like this:

ActiveRecord::Base.connection.execute('ALTER TABLE tbl AUTO_INCREMENT = 1000');

Or from any descendant of ActiveRecord::Base (i.e. any model class), like this:

MyRecord.connection.execute('ALTER TABLE tbl AUTO_INCREMENT = 1000')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文