Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a table with id: :string on SQLite crashes with duplicated sqlite_autoindex #33320

Closed
pudiva opened this issue Jul 9, 2018 · 0 comments
Closed

Comments

@pudiva
Copy link

@pudiva pudiva commented Jul 9, 2018

Steps to reproduce

Run this:

#!/bin/sh
rails new sqlite_autoindex
cd sqlite_autoindex
bundle exec rails g scaffold broken
patch db/migrate/* <<EOF
--- 20180709003138_create_brokens.rb    2018-07-08 21:32:20.270221417 -0300
+++ db/migrate/20180709003138_create_brokens.rb 2018-07-08 21:32:39.173220733 -0300
@@ -1,6 +1,6 @@
 class CreateBrokens < ActiveRecord::Migration[5.2]
   def change
-    create_table :brokens do |t|
+    create_table :brokens, id: :string do |t|

       t.timestamps
     end
EOF
bundle exec rake db:migrate
bundle exec rake test

Expected behavior

Tests passing

Actual behavior

rails aborted!
ArgumentError: Index name 'sqlite_autoindex_brokens_1' on table 'brokens' already exists

Other considerations

The migrations run correctly and yield the following db/schema.rb:

ActiveRecord::Schema.define(version: 2018_07_09_002932) do

  create_table "brokens", id: :string, force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["id"], name: "sqlite_autoindex_brokens_1", unique: true
  end

end

and running:

RAILS_ENV=test bundle exec rake db:migrate

will get past this error. So teh problem is that this sqlite_autoindex_brokens_1 is automatically created when creating the primary key, but it gets dumped into a separate statement on schema.rb, ending as a duplicate.

I wonder why it doesn't happen with integer keys. Anyway, it shouldn't be dumped to schema.rb.

System configuration

Stuff Version
Rails version Rails 5.2.0
Ruby version ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
SQLite version sqlite-3.24.0
SQLite gem version sqlite3 (1.3.13)

<3

@kamipo kamipo closed this in 544492d Jul 9, 2018
kamipo added a commit that referenced this issue Jul 9, 2018
Related #31201.

If creating custom primary key (like a string) in SQLite, it would also
create an internal index implicitly which named begin with "sqlite_".

It need to be hidden since the internal object names are reserved and
prohibited for public use.

See https://www.sqlite.org/fileformat2.html#intschema

Fixes #33320.
pawelpacana added a commit to RailsEventStore/rails_event_store that referenced this issue Jan 21, 2019
     ActiveRecord::StatementInvalid:
       SQLite3::SQLException: index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped: DROP INDEX "sqlite_autoindex_event_store_events_1"

Related:
rails/rails@7fae8e3
rails/rails#33320
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant