ruby - 为什么保存在这里失败,SQLite3::ConstraintException: column user_id is not unique: INSERT INTO "pins"?

标签 ruby sqlite devise ruby-on-rails-4 paperclip

我正在将 Devise 和 Paperclip 与 Rails 4 应用程序一起使用。

我正在尝试找出无法为同一用户添加另一个 pin 的原因。据我从错误中了解到,它具有用户独特性。我对吗?如何修复它?

登录后,我可以创建第一个带有图像和描述的图钉,但无法创建第二个图钉。我收到一个错误;

SQLite3::ConstraintException: column user_id is not unique: INSERT INTO "pins" ("created_at", "description", "image_content_type", "image_file_name", "image_file_size", "image_updated_at", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?)

这是终端日志:

Started POST "/pins" for 127.0.0.1 at 2013-11-25 03:32:11 -0500
Processing by PinsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxxxxxxx", "pin"=>{"image"=>#<ActionDispatch::Http::UploadedFile:000000000000 @tempfile=#<Tempfile:/var/folders/z0/7gq1xnhs0051g0>, @original_filename="cheer_up.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"pin[image]\"; filename=\"cheer_up.png\"\r\nContent-Type: image/png\r\n">, "description"=>"cheer up!"}, "commit"=>"Create Pin"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 6 ORDER BY "users"."id" ASC LIMIT 1
   (0.1ms)  begin transaction
Binary data inserted for `string` type on column `image_content_type`
  SQL (0.5ms)  INSERT INTO "pins" ("created_at", "description", "image_content_type", "image_file_name", "image_file_size", "image_updated_at", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", Mon, 25 Nov 2013 08:32:11 UTC +00:00], ["description", "cheer up!"], ["image_content_type", "image/png"], ["image_file_name", "cheer_up.png"], ["image_file_size", 211523], ["image_updated_at", Mon, 25 Nov 2013 08:32:11 UTC +00:00], ["updated_at", Mon, 25 Nov 2013 08:32:11 UTC +00:00], ["user_id", 6]]
SQLite3::ConstraintException: column user_id is not unique: INSERT INTO "pins" ("created_at", "description", "image_content_type", "image_file_name", "image_file_size", "image_updated_at", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?)
   (0.1ms)  rollback transaction
Completed 500 Internal Server Error in 6ms

ActiveRecord::RecordNotUnique (SQLite3::ConstraintException: column user_id is not unique: INSERT INTO "pins" ("created_at", "description", "image_content_type", "image_file_name", "image_file_size", "image_updated_at", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?)):
  app/controllers/pins_controller.rb:25:in `block in create'
  app/controllers/pins_controller.rb:24:in `create'

这是我的 Pin 模型。

class Pin < ActiveRecord::Base

  # associations
  belongs_to :user
  has_attached_file :image

  # validations
  validates_presence_of :description, :user_id
  validates_attachment :image, presence: true,
                              content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif' ] },
                              size: { less_than: 3.megabytes } 
end

Devise 的用户模型

class User < ActiveRecord::Base

  # Virtual attribute for authenticating by either username or email
  # This is in addition to a real persisted field like 'username'
  attr_accessor :login

  has_many :pins

  #https://github.com/thoughtbot/paperclip#quick-start
  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, #:recoverable
         :rememberable, :trackable, :validatable

  # https://github.com/plataformatec/devise/wiki/How-To%3a-Allow-users-to-sign-in-using-their-username-or-email-address#tell-devise-to-use-login-in-the-authentication_keys
  def self.find_first_by_auth_conditions(warden_conditions)
    conditions = warden_conditions.dup
    if login = conditions.delete(:login)
      where(conditions).where(["username = :value OR lower(email) = lower(:value)", { :value => login }]).first
    else
      where(conditions).first
    end
  end        
end

这是我的架构

ActiveRecord::Schema.define(version: xxxxxxxxxx) do

  create_table "pins", force: true do |t|
    t.string   "description"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "user_id"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
  end

  add_index "pins", ["user_id"], name: "index_pins_on_user_id", unique: true

  create_table "users", force: true do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "username"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  add_index "users", ["username"], name: "index_users_on_username", unique: true

end

最佳答案

它失败是因为您的数据库中有唯一性约束:

查看架构或迁移文件:

add_index“pins”,[“user_id”],名称:“index_pins_on_user_id”,唯一:true

如果您认为不需要它,并且由于 User 模型中有一个 has_many :pins ,这似乎是错误的,您可以创建一个迁移到删除不必要的索引:

remove_index(:pins, :name => 'index_pins_on_user_id')

关于ruby - 为什么保存在这里失败,SQLite3::ConstraintException: column user_id is not unique: INSERT INTO "pins"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20188107/

相关文章:

ruby - RSpec 和测试传递给脚本的命令行参数

ruby-on-rails - :id:Symbol的未定义方法 'map'

c# - 如何使用 sqlCommand.ExecuteReader(); 返回数据?

ios - Swift - 如何检查现有的 Core Data Store iOS

ruby-on-rails - Rails 3 Devise 停止使用更多加密 "stretches"

ruby-on-rails - 设计 Rspec 注册 Controller 测试在更新时失败,就像它试图确认电子邮件地址一样

ruby - 杰基尔 3.1.6 |错误:未初始化常量 Jekyll::Filters::URLFilters

ruby - 具有散列的方法和未运行的 proc 参数

java - 将数据插入 SQLite

ruby-on-rails - Rails 中的唯一 session ID