Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy Save fails here, SQLite3::ConstraintException: column user_id is not unique: INSERT INTO "pins"?
    text
    copied!<p>I'm using Devise and Paperclip with Rails 4 app. </p> <p>I'm trying to figure out the cause for failing to add another pin with same user. As I understand from the error, it has something with user uniqueness. Am I right? How to fix it? </p> <p>After I logged in, I'm able to create first pin with an image and description, but I can't creat second pin. I got an error; </p> <pre><code>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 (?, ?, ?, ?, ?, ?, ?, ?) </code></pre> <p>Here is the terminal log:</p> <pre><code>Started POST "/pins" for 127.0.0.1 at 2013-11-25 03:32:11 -0500 Processing by PinsController#create as HTML Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"xxxxxxxx", "pin"=&gt;{"image"=&gt;#&lt;ActionDispatch::Http::UploadedFile:000000000000 @tempfile=#&lt;Tempfile:/var/folders/z0/7gq1xnhs0051g0&gt;, @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"&gt;, "description"=&gt;"cheer up!"}, "commit"=&gt;"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' </code></pre> <p>Here is my Pin model.</p> <pre><code>class Pin &lt; 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 </code></pre> <p>User model by Devise</p> <pre><code>class User &lt; 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 =&gt; { :medium =&gt; "300x300&gt;", :thumb =&gt; "100x100&gt;" }, :default_url =&gt; "/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 =&gt; login }]).first else where(conditions).first end end end </code></pre> <p>And here is my Schema</p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload