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

查看:69
本文介绍了如何通过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天全站免登陆