生成具有参考的脚手架时出错

时间:2016-05-02 18:23:55

标签: ruby-on-rails ruby rails-migrations

我正在尝试生成3个支架:

  

$ rails g scaffold艺术家姓名:字符串类型:字符串bio:text resume:string    site:string

     

$ rails g scaffold ArtistSerie title:string artist:references

     

$ rails g scaffold ArtistSeriePhoto照片:string    title:string year:integer description:text dimensions:string    特色:boolean artist_serie:references

前两个模型正在正确创建索引和外键,但第三个模型在rake db:migrate之后生成此错误:

Mysql2::Error: Key column 'artist_series_id' doesn't exist in table: ALTER TABLE `artist_serie_photos` ADD CONSTRAINT `fk_rails_9422e9e931`
FOREIGN KEY (`artist_series_id`)
  REFERENCES `artist_series` (`id`)

这是生成的迁移:

class CreateArtists < ActiveRecord::Migration
  def change
    create_table :artists do |t|
      t.string :name
      t.string :type
      t.text :bio
      t.string :resume
      t.string :site

      t.timestamps null: false
    end
  end
end


class CreateArtistSeries < ActiveRecord::Migration
  def change
    create_table :artist_series do |t|
      t.string :title
      t.references :artist, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end


class CreateArtistSeriePhotos < ActiveRecord::Migration
  def change
    create_table :artist_serie_photos do |t|
      t.string :photo
      t.string :title
      t.integer :year
      t.text :description
      t.string :dimensions
      t.boolean :featured
      t.references :artist_serie, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end

表已创建且字段为artist_serie_id,但索引和外键却没有。

我已经创建了另一个空白项目,它可以工作(在sqlite上),所以可能是mysql适配器错误。

有什么想法吗?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我希望根问题是series is singular。也就是说,serie不是series的单数形式。将series替换为sequence可能值得。