使用引用生成脚手架时出错
Error on generate scaffolds with references
我正在尝试生成 3 个支架:
$ rails g scaffold Artist name:string type:string bio:text resume:string
site:string
$ rails g scaffold ArtistSerie title:string artist:references
$ rails g scaffold ArtistSeriePhoto photo:string
title:string year:integer description:text dimensions:string
featured: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
table 已创建,字段 artist_serie_id 也已创建,但索引和外键未创建。
我已经创建了另一个空白项目并且它可以工作(在 sqlite 上)所以可能是 mysql 适配器错误。
有什么想法吗?
感谢您的帮助!
我预计根本问题是 series
is singular。也就是说,serie
不是 series
的单数形式。可能值得将 series
替换为 sequence
。
我正在尝试生成 3 个支架:
$ rails g scaffold Artist name:string type:string bio:text resume:string site:string
$ rails g scaffold ArtistSerie title:string artist:references
$ rails g scaffold ArtistSeriePhoto photo:string title:string year:integer description:text dimensions:string featured: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
table 已创建,字段 artist_serie_id 也已创建,但索引和外键未创建。
我已经创建了另一个空白项目并且它可以工作(在 sqlite 上)所以可能是 mysql 适配器错误。
有什么想法吗?
感谢您的帮助!
我预计根本问题是 series
is singular。也就是说,serie
不是 series
的单数形式。可能值得将 series
替换为 sequence
。