ruby-on-rails – Postgres Rails适配器正在考虑Bigint / Varchar是整数 – 在保存时导致异常

我有一个数字模型,一个数字字段是一个bigint. schema.rb文件正确地创建数据库中的表结构.

但是使用该应用程序,当我去创建一个新的数字,我得到一个错误说:

RangeError at /numbers
71731224865 is out of range for ActiveRecord::ConnectionAdapters::Postgresql::OID::Integer with limit 4

为什么这个数字字段仍然被视为标准整数而不是bigint?

这似乎不同于StackOverflow上的其他“超出范围”错误,因为它们似乎无法在数据库中将字段定义为bigint.但是,我有这个..这似乎是“保存”适配器被吓倒了.

这是在schema.rb中显示的create_table:

create_table "numbers",id: false,force: :cascade do |t|
  t.bigint   "number",null: false,index: {name: "index_numbers_on_number",unique: true}
  t.string   "formatted_number"
  t.text     "description"
  t.integer  "user_id",index: {name: "index_numbers_on_userid"},foreign_key: {name: 'fk_numbers_user_id'}
  t.datetime "created_at",null: false
  t.datetime "updated_at",null: false
end

更新1:我已经重新创建了一个DECIMAL(11,0)的列,希望这将是一个临时的解决方法,但失败了同样的错误!也许唯一的选择是VORKAR(11).

更新2:好的有些奇怪的事情正在发生.我现在将该字段定义为VARCHAR(11),所以我可以继续进行工作…但是失败也有同样的错误.什么?

更新3:可能是因为数字表中的数字字段是主键?我没有使用id作为关键,我已经超越了它.不是我试图将该字段用作VARCHAR,这对PostgresqlAdaptor仍然显示为止没有任何意义… ActiveRecord :: ConnectionAdapters :: Postgresql :: OID :: Integer超出范围限制4错误.

这是我的号码模型,如果它有帮助:

class Number < AbstractModel
  belongs_to :user
  has_many :extensions

  self.primary_key = 'number'
  validates :number,numericality: { only_integer: true,greater_than_or_equal_to: 611,less_than_or_equal_to: 61999999999 },presence: true,uniqueness: true
  validates :user_id,presence: true
end

更新4:使用Rails控制台查看data type it thinks the columns是什么,它说INTEGER!格儿. sql_type确实返回BIGINT.什么?

Loading development environment (Rails 4.2.1)
irb(main):001:0> Number.column_for_attribute('number').type
  => :integer
irb(main):002:0> Number.column_for_attribute('number').sql_type
  => "bigint"
irb(main):003:0> quit

确保数据库仍按预期设置:

[turgs@web123 myapp]$psql -h 127.0.0.1 -p 5432
psql (9.1.15)

db=> \d numbers
                   Table "public.numbers"
      Column      |            Type             | Modifiers 
------------------+-----------------------------+-----------
 number           | bigint                      | not null
 formatted_number | character varying           | 
 description      | text                        | 
 user_id          | integer                     | not null
 max_extn_length  | integer                     | 
 created_at       | timestamp without time zone | not null
 updated_at       | timestamp without time zone | not null
Indexes:
    "index_numbers_on_number" UNIQUE,btree (number)
    "index_numbers_on_userid" btree (user_id)
Referenced by:
    TABLE "extensions" CONSTRAINT "fk_extensions_number_id" FOREIGN KEY (number_id) REFERENCES numbers(number)

db=>

更新5:是….另一个更新!这一次我以为我会摔在我的剑上,尝试大家在其他帖子中使用的位置,在那里他们无法在数据库中首先创建BIGINT.所以,我改变了schema.rb到:

create_table "numbers",force: :cascade do |t|
  t.integer  "number",limit: 8,foreign_key: {name: 'fk_numbers_user_id'}
  t.integer  "max_extn_length"
  t.datetime "created_at",null: false
end

注意t.integer“number”,limit:8.令人惊讶的是,DID在数据库中创建一个bigint字段.我的希望很高.不幸的是,没有雪茄.保存值时出现相同的错误.

解决方法

尝试
t.column :number,:bigint

见:
Rails Migration: Bigint on PostgreSQL seems to be failing?

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...