Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails I';在使用Rails 4.1时,如何将新模型id类型设置为bigint Postgres_Ruby On Rails_Ruby_Postgresql - Fatal编程技术网

Ruby on rails I';在使用Rails 4.1时,如何将新模型id类型设置为bigint Postgres

Ruby on rails I';在使用Rails 4.1时,如何将新模型id类型设置为bigint Postgres,ruby-on-rails,ruby,postgresql,Ruby On Rails,Ruby,Postgresql,我不能在我的模型id中使用bigint,因为我使用的是Rails 4.1 我希望我的模型有一个正常的自动递增id,但我希望它是一个bigint而不是一个正则整数 事务表将包含数百万条记录,稍后迁移ID将是一件令人头痛的事情,需要停机 我首先尝试使用rails生成器(就像一个好的懒惰开发人员) 这给了我 class CreateTransactions < ActiveRecord::Migration def change create_table :transactions

我不能在我的模型id中使用
bigint
,因为我使用的是Rails 4.1

我希望我的模型有一个正常的自动递增id,但我希望它是一个bigint而不是一个正则整数

事务表将包含数百万条记录,稍后迁移ID将是一件令人头痛的事情,需要停机

我首先尝试使用rails生成器(就像一个好的懒惰开发人员)

这给了我

class CreateTransactions < ActiveRecord::Migration
  def change
    create_table :transactions do |t|
      t.bigint :id
      #more stuff omitted
    end
  end
end

这很有效。迁移正在运行,但随后它无法自动增加id。这意味着,我无法执行
事务。创建

或者它从db层向我大喊关于id上的
null
约束

显然,您可以创建一个表,然后使用
change\u列
对其进行修改,但在
schema.rb中没有反映,这让我很担心

同样明显的是,^^(
change\u列
on id)是一个不可逆转的迁移,所以我想避免这种情况

我知道一定有一个简单/易怒、好的方法来实现这一点

预期结果:

Transaction.create


给我一个新的事务,其中id将自身设置为正常,但它是一个大整数
8位
而不是一个正常整数
4位

我只是通过阅读postgres支持的数据类型和猜测rails将如何处理它来理解它

通过直接连接到postgres进行验证

                                                              Table "public.transactions"
    Column     |            Type             | Collation | Nullable |                       Default                       | Storage  | Stats target | Description
---------------+-----------------------------+-----------+----------+-----------------------------------------------------+----------+--------------+-------------
 id            | bigint                      |           | not null | nextval('ghost_card_transactions_id_seq'::regclass) | plain    |              |
create_table :transactions, id: false do |t|
  t.bigint :id
end
create_table :transactions, id: :bigserial do |t|
  #other model stuff here
end
                                                              Table "public.transactions"
    Column     |            Type             | Collation | Nullable |                       Default                       | Storage  | Stats target | Description
---------------+-----------------------------+-----------+----------+-----------------------------------------------------+----------+--------------+-------------
 id            | bigint                      |           | not null | nextval('ghost_card_transactions_id_seq'::regclass) | plain    |              |