将默认的ActiveRecord ID从0更改为1000000 [英] Changing the default ActiveRecord id from 0 to 1000000

查看:91
本文介绍了将默认的ActiveRecord ID从0更改为1000000的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将已创建对象的ID的最小值从1更改为1000。
因此,当我在rails中创建我的第一个模型对象时,它获得的ID是1000,而不是1。

I'd like to change the minimum for the id of created objects from 1 to 1000. So when I create in rails my first model object it gets the ID 1000 and not 1.

是否可以在模式/迁移文件中进行设置?

Is there a way to set this in the schema/migration files?

推荐答案

I我对MySQL不熟悉,但是对于Postgres,您可以在迁移文件中执行以下操作:

I am not familiar with MySQL, but for Postgres you can do something like this in your migration file:

class CreateCustomers < ActiveRecord::Migration
  def self.up
    create_table :customers do |t|
      t.string :name
      t.timestamps

    end
    execute "SELECT setval('customers_id_seq', 1000);"
end

请注意,execute方法调用是在create_table块之外进行的。

Note that the execute method call is made outside the create_table block.

这篇关于将默认的ActiveRecord ID从0更改为1000000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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