【发布时间】:2016-08-27 14:39:32
【问题描述】:
我正在尝试生成 3 个脚手架:
$ rails g 脚手架艺术家姓名:字符串类型:字符串生物:文本简历:字符串 网站:字符串
$ rails g scaffold ArtistSerie 标题:字符串艺术家:参考
$ rails g scaffold ArtistSeriePhoto 照片:字符串 标题:字符串年份:整数描述:文本尺寸:字符串 特色:布尔艺术家系列:参考
前两个模型正确地创建了它们的索引和外键,但是第三个模型在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 适配器错误。
有什么想法吗?
感谢您的帮助!
【问题讨论】:
-
我预计根本问题是
series是单数en.wikipedia.org/wiki/Series。也就是说,serie不是series的单数形式。将series替换为sequence可能值得。 -
是啊!!!那就是问题所在!!谢谢!
-
如果您愿意,请创建一个答案,我会接受它:)
-
我情不自禁地接受你。 :)
标签: ruby-on-rails ruby rails-migrations