【问题标题】:Migration a has_and_belongs_to_many join table don't create the table迁移 has_and_belongs_to_many 连接表不会创建表
【发布时间】:2011-12-02 18:49:38
【问题描述】:

我正在使用 Rails 3.0.9 和 Postgres 9.4 开发一个 Web 应用程序
我正在尝试为 has_and_belongs_to_many 关联创建一个连接表,但是当执行“rake db:migrate”时,唯一未执行的迁移是连接表的迁移。 Rails 没有显示任何错误,只是没有创建表。 当我进行回滚时,rails 显示错误,因为无法删除表,因为不存在。

这里是迁移的代码:

class CreateCampanaLocalJoinTable < ActiveRecord::Migration
  def self.up
    def change
      create_table :campanas_locals, :id => false do |t|
        t.integer :campana_id
        t.integer :local_id
      end
    end
  end

  def self.down
    drop_table :campanas_locals
  end
end

有人有想法吗?谢谢!

【问题讨论】:

  • 您已经在 up 方法中嵌套了一个方法定义。这不是真的执行 change 方法吗,因为它只是该方法的定义,而不是对其的调用?

标签: ruby-on-rails migration associations has-and-belongs-to-many


【解决方案1】:

Rails 3.0.X 试用:

class CreateCampanaLocalJoinTable < ActiveRecord::Migration
  def self.up
    create_table :campanas_locals, :id => false do |t|
      t.integer :campana_id
      t.integer :local_id
    end
  end

  def self.down
    drop_table :campanas_locals
  end
end

Rails 3.1.X 试试:

class CreateCampanaLocalJoinTable < ActiveRecord::Migration
  def change
    create_table :campanas_locals, :id => false do |t|
      t.integer :campana_id
      t.integer :local_id
    end
  end
end

【讨论】:

  • 我已经尝试过 3.0.x 和 3.1.X 格式,但都不起作用。我有 2 个针对 2 个 has_and_belongs_to_many 关联的迁移,并且都有问题。我手动创建表以继续工作,关联有效,但迁移不起作用。谢谢
猜你喜欢
  • 2011-05-21
  • 2013-07-19
  • 2021-10-06
  • 2017-05-07
  • 2014-02-28
  • 2016-05-20
  • 1970-01-01
  • 2016-05-28
  • 2020-04-18
相关资源
最近更新 更多