{"issue":{"created_at":"2011-10-10T09:58:51Z","state":"closed","updated_at":"2011-12-13T17:09:40Z","user":{"gravatar_id":"e29f04f339715955060858e0e65e1311","id":128151,"url":"https://api.github.com/users/fphilipe","login":"fphilipe","avatar_url":"https://secure.gravatar.com/avatar/e29f04f339715955060858e0e65e1311?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"comments":2,"body":"Creating a record that violates a unique constraint in a SQLite database should raise `ActiveRecord::RecordNotUnique`.\r\n\r\nWhat I observed is the following:\r\n\r\n```ruby\r\nTestModel.create(name: 'test')  # => persisted record\r\n\r\nx = TestModel.new(name: 'test')\r\nx.save  # => ActiveRecord::StatementInvalid: SQLite3::ConstraintException: constraint failed\r\nx.save  # => ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: column name is not unique\r\nx.save  # => ActiveRecord::StatementInvalid: SQLite3::ConstraintException: constraint failed\r\nx.save  # => ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: column name is not unique\r\n```\r\n\r\nThe raised exception alternates starting with `ActiveRecord::StatementInvalid` followed by `ActiveRecord::RecordNotUnique`.\r\n\r\nExecuting raw SQL always raises `ActiveRecord::RecordNotUnique` as expected:\r\n\r\n```ruby\r\nTestModel.connection.execute('INSERT INTO \"test_models\" (\"name\") VALUES (\"test\")')\r\n# => ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: column name is not unique\r\nTestModel.connection.execute('INSERT INTO \"test_models\" (\"name\") VALUES (\"test\")')\r\n# => ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: column name is not unique\r\n```\r\n\r\nThe app was created with `rails new someapp` and the migration for the model used is:\r\n\r\n```ruby\r\nclass CreateTestModels < ActiveRecord::Migration\r\n  def change\r\n    create_table :test_models do |t|\r\n      t.string :name\r\n\r\n      t.timestamps\r\n    end\r\n\r\n    add_index :test_models, :name, unique: true\r\n  end\r\nend\r\n```\r\n\r\nAnyone else observing this weird behavior?","assignee":null,"id":1862529,"pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"number":3277,"labels":[],"title":"ActiveRecord::RecordNotUnique not always raised","html_url":"https://github.com/rails/rails/issues/3277","closed_at":"2011-12-13T17:09:40Z","url":"https://api.github.com/repos/rails/rails/issues/3277","milestone":null},"action":"created","comment":{"created_at":"2011-12-13T17:09:40Z","updated_at":"2011-12-13T17:09:40Z","user":{"gravatar_id":"e29f04f339715955060858e0e65e1311","id":128151,"url":"https://api.github.com/users/fphilipe","login":"fphilipe","avatar_url":"https://secure.gravatar.com/avatar/e29f04f339715955060858e0e65e1311?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"body":"I can no longer reproduce this alternating behavior. It is still raising the wrong exception though which I will either discuss in #2965 or in a new ticket.","id":3125915,"url":"https://api.github.com/repos/rails/rails/issues/comments/3125915"}}