【发布时间】:2012-07-03 06:35:15
【问题描述】:
我在 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
【问题讨论】:
标签: ruby-on-rails sqlite ruby-on-rails-3.2