Why do I keep getting an error when starting rake db: migrate on my new rails instance?

Why do I keep getting the error shown below when running rake db:migrate ...

SQLite3 :: CantOpenException: unable to open database file: CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email") / cygdrive / c / users / daniel / workspace / ruby ​​/ rails / tesT_app / db / migrate / 20130606041329_devise_create_users. : 40: in `change '

I followed the letter https://github.com/plataformatec/devise#getting-started in github readme development, created a completely new project and still got an error,

Created a new application for rails : (success)
rails new tesT_app

Added gem 'devise' to my gemfile : (success)
gem 'devise'

Installed package : (success)
bundle install

Ran rail generator : (success)
rails generate devise: install

Generated development model named User : (success)
rails generate devise User

I mentioned the SO question in SQLite3: CantOpenException (uanble to open the database file) , which made me try using rake db:create , which returned db/development.sqlite3 already exists . well.

Then I ran rake db:migrate to get started. : (fail)
With SQLite3::CantOpenException: unable to open database file: CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email")/cygdrive/c/users/daniel/workspace/ruby/rails/tesT_app/db/migrate/20130606041329_devise_create_users.rb:40:in error SQLite3::CantOpenException: unable to open database file: CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email")/cygdrive/c/users/daniel/workspace/ruby/rails/tesT_app/db/migrate/20130606041329_devise_create_users.rb:40:in change'`

What's happening? I follow this beginning of the letter and cannot understand this!

I would really appreciate it.

edit : String: 40 to ... create_users.rb is
add_index :users, :email, :unique => true

and note that I didn’t touch anything.

+4
source share
3 answers

In SQLite, the uniqueness of the index name is applied at the database level. In MySQL, you will not reproduce the same problem. You can change the index name or comment on this line (and return when deployed during production) or change the database (I mean using mysql instead).

+3
source

Sorry, I thought you had a separate user model. Could you host the migration file? Also try running

 rake db:drop 

(to remove the current db), then

 rake db:create and rake db:migrate 

This will allow you to upgrade to a new db.

+1
source

I had a similar problem, and I suggest following this path. Migrate in two steps: for example, suppose you have a migration called users. Transfer it as usual, but here is not yet unique.

 class CreateUsers < ActiveRecord::Migration def change create table :users do |t| t.integer :name t.integer :email t.timestamps end end end 

Migration with:

 $ bundle exec rake db:migrate 

After the migration, create another migration:

 $ rails generate migration add_index_users_name_email 

In this migration file, add uniqueness:

 class AddIndexUsersNameEmail < ActiveRecord::Migration def change add_index :users, :name, unique: true add_index :users, :email, unique: true end end 

Reschedule as usual, and this should work and will not generate errors, because, as Ivan said, SQLite provides uniqueness at the db level, and thus solves the problem.

Another way to solve this problem, if you still throw errors, is to create a separate migration for each record. For instance:

 $ rails generate migration add_index_users_name $ rails generate migration add_index_users_email class AddIndexUsersName < ActiveRecord::Migration def change add_index :users, :name, unique: true end end class AddIndexUsersEmail < ActiveRecord::Migration def change add_index :users, :email, unique: true end end 

Let me know if your problem solves

0
source

Source: https://habr.com/ru/post/1484756/


All Articles