我在 Rails 中创建了多对多关系,这是我的模型和迁移
class Channel < ActiveRecord::Base
has_and_belongs_to_many :packages
validates_presence_of :name
end
class Package < ActiveRecord::Base
has_and_belongs_to_many :channels
validates_presence_of :name
end
class CreateChannelsPackages < ActiveRecord::Migration
def change
create_table :channels_packages, :id => false do |t|
t.references :channel
t.references :package
t.timestamps
end
add_index :channels_packages, :channel_id
add_index :channels_packages, :package_id
end
end
然后我有一个多项选择,但是当我尝试保存时出现此错误
SQLite3::ConstraintException: constraint failed: INSERT INTO "channels_packages" ("package_id", "channel_id") VALUES (1, 1)
我试图从迁移中删除索引但它没有解决它,其他人有这个问题吗?
顺便说一句,我正在使用 Rails 3.2.6 和 sqlite3 1.3.6