我有三个模型:Book
,genre
,BookGenre
,它们之间存在以下关系:
class BookGenre < ActiveRecord::Base
belongs_to :book
belongs_to :genre
end
class Book < ActiveRecord::Base
has_many :book_genres
has_many :genres, through: :book_genres
end
class Genre < ActiveRecord::Base
has_many :book_genres
has_many :books, through: :book_genres
end
接下来我使用seed
文件将数据放入这些表中。
但当我想再次执行rake db:seed
时,它显示了以下错误
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: update or delete on table "books" violates foreign key constraint "fk_rails_4a117802d7" on table "book_genres"
DETAIL: Key (id)=(10) is still referenced from table "book_genres".
在我的 seed.rb 文件中。
Book.destroy_all
Genre.destroy_all
...create data