【问题标题】:Database field refuses to be an integer - Rails/Postgres数据库字段拒绝为整数 - Rails/Postgres
【发布时间】:2014-02-27 10:42:46
【问题描述】:

我正在创建一个连接表,大致遵循 Railscast:http://railscasts.com/episodes/17-habtm-checkboxes-revised?view=asciicast

我无法在对象上设置 has_many 记录并得到以下错误:

2.0.0p353 :012 > invoice.fly_ids
   (0.9ms)  SELECT "flies".id FROM "flies" INNER JOIN "categorizations" ON "flies"."id" = "categorizations"."fly_id" WHERE "categorizations"."invoice_id" = 1
ActiveRecord::StatementInvalid: PG::Error: ERROR:  operator does not exist: integer = character varying
LINE 1: ...ies" INNER JOIN "categorizations" ON "flies"."id" = "categor...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "flies".id FROM "flies" INNER JOIN "categorizations" ON "flies"."id" = "categorizations"."fly_id" WHERE "categorizations"."invoice_id" = 1

我无法让它工作,我认为这是因为“分类”表使用的是 varchar 而不是整数。

我的迁移文件如下所示:

class CreateCategorizations < ActiveRecord::Migration
  def change
    create_table :categorizations do |t|
      t.integer :user_id
      t.integer :fly_id

      t.timestamps

      add_index :categorizations, :user_id
      add_index :categorizations, :fly_id
    end
  end
end

但是,当我查看创建的数据库表时,:user_id 和 :fly_id 都是 varchar

当我在迁移文件中指定整数时,为什么将这些字段创建为 varchar?

(即使我得到这个工作,它可能无法解决问题......)

编辑:

用户模型:

class User < ActiveRecord::Base

....
has_many :invoices
....
end

发票型号:

class Invoice < ActiveRecord::Base
  attr_accessible :active
  validates :user_id, presence: true
  belongs_to :user

  has_many :categorizations
  has_many :flies, through: :categorizations
end

发票迁移:

class CreateInvoices < ActiveRecord::Migration
  def change
    create_table :invoices do |t|
      t.boolean :active
      t.integer :user_id

      t.timestamps
    end
    add_index :invoices, :user_id
  end

end

分类模型:

class Categorization < ActiveRecord::Base
  attr_accessible :fly_id, :user_id

  belongs_to :invoice
  belongs_to :fly
end

分类迁移:

class CreateCategorizations < ActiveRecord::Migration
  def change
    create_table :categorizations do |t|
      t.integer :user_id
      t.integer :fly_id

      t.timestamps

      add_index :categorizations, :user_id
      add_index :categorizations, :fly_id
    end
  end
end

飞行模型:

class Fly < ActiveRecord::Base
  attr_accessible :description, :name
  validates :description, :name, presence: true

  has_many :categorizations
  has_many :invoices, through: :categorizations
end

飞行迁徙:

class CreateFlies < ActiveRecord::Migration
  def change
    create_table :flies do |t|
      t.string :name
      t.string :description

      t.timestamps
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 postgresql has-and-belongs-to-many


    【解决方案1】:

    HABTM

    如果您使用HABTM,则您的表不需要任何主键 (id):

    create_table :invoices_flies, :id => false do |t|
      t.references :user
      t.references :flies
    end
    

    has_many:通过

    如果您使用has_many :through,您的表将使用主键,因为它将是自己的模型:

    #app/models/user.rb
    Class User < ActiveRecord::Base
        has_many :categorizations
        has_many :flies, through: :categorizations
    end
    
    #app/models/fly.rb
    Class Fly < ActiveRecord::Base
        has_many :categorizations
        has_many :users, through: :categorizations
    end
    
    #app/models/categorization.rb
    Class Categorization < ActiveRecord::Base
        belongs_to :fly
        belongs_to :user
    end
    
    create_table :categorizations do |t|
      t.integer :user_id
      t.integer :fly_id
    end
    

    我认为您的问题可能与您的关联结构有关 - 您能否提供一些有关您如何设置它们的信息?

    【讨论】:

      猜你喜欢
      • 2016-01-31
      • 2016-08-12
      • 2015-07-10
      • 2021-09-24
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多