如何通过 rails 迁移在 Postgres 中设置主键 (ID) 列的起点 [英] How can I set the starting point for the primary key (ID) column in Postgres via a rails migration

查看:19
本文介绍了如何通过 rails 迁移在 Postgres 中设置主键 (ID) 列的起点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个 Rails 应用程序部署到使用 PostgreSQL 作为其后端的 heroku.在我的数据库迁移中,我通常将报告等的 ID 字段设置为至少 1000,大多数客户似乎不喜欢从 1 开始.

I am deploying a rails app to heroku which uses PostgreSQL as its back-end. In my database migration I normally set the ID field for things likes reports etc to at least 1000, most clients don't seem to like starting at 1.

通常我使用 mysql,我只是在创建表后添加一个特定的 sql:

Normally I use mysql and I simply add an sql specific after my table creation:

def self.up
    create_table :reports do |t|
       t.references :something
       ...
    end
    execute("ALTER TABLE reports AUTO_INCREMENT = 1000;")
end

有谁知道我如何为 PostgreSQL 实现相同的目标,理想情况下,我希望迁移来构建表本身,这样就不是特定于数据库的.

Does anybody know how I can acheive the same for PostgreSQL, ideally I would like the migration to build the table itself so that's not DB specific.

我想实现目标的一种愚蠢方法是循环创建和删除 999 条记录,哎呀.

I guess a silly way of achieving my goal would be to create and delete 999 records in a loop, ouch.

推荐答案

不知道红宝石和铁路部分,但你说的查询是

Have no idea about rubies and railroads part, but query you're talking about is

ALTER SEQUENCE reports_something_seq RESTART 1000;

您必须在您的表中查找序列名称和 postgresql 文档,以获取有关此事的一般教育;-)

You will have to look up your table for the sequence name and postgresql documentation for general education regarding the matter ;-)

这篇关于如何通过 rails 迁移在 Postgres 中设置主键 (ID) 列的起点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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