Here's what I did successfully to install Devise:
I put in the devise gem
Ran bundle install
Installed devise with rails generate devise:install
Ran rake db:migrate (successfully) just to be safe
Now when I try to do rails generate devise User I get the following error:
== 20190915133638 AddDeviseToUsers: migrating =================================
-- change_table(:users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "users" does not exist
: ALTER TABLE "users" ADD "email" character varying DEFAULT '' NOT NULL
I tried this adding a few custom fields to the devise migration, but then again with the autogenerated migration and got the same result either way.
Can anyone see how I'm getting this error? I've tried rake db:reset db:create db:migrate but to no avail.
Did you have a User model before? It seems that devise thought that too and it's trying to add its columns to a non-existent User model.
This is what I would do...
Run rails db:drop
Delete db/schema.rb file.
Delete db/migrate/20190915133638_add_devise_to_users.rb
Run rails generate devise:install again
Run rails db:migrate
EDIT
If the previous steps didn't work then try to create a users table before devise's migration.
For this follow these steps:
rails g model User
Change the name of the migration file so it will be executed right before the AddDeviseToUsers migration.
Run rails db:migrate
Try db:drop instead of db:reset
Related
when I go to my http://localhost:3000/ I am getting the following:
ActiveRecord::PendingMigrationError
Migrations are pending. To resolve this issue, run: bin/rails db:migrate RAILS_ENV=development
Extracted source:
# Raises <tt>ActiveRecord::PendingMigrationError</tt> error if any migrations are pending.
def check_pending!(connection = Base.connection)
raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration?(connection)
end
def load_schema_if_pending!
Also, when I tried to to the heroku run rake db:migrate in the console, it said:
StandardError: An error has occurred, this and all later migrations
canceled: PG::DuplicateColumn: ERROR: column "email" of relation
"users" already exists
I am new to ruby and followed the devise tutorial by Mackenzie Child. It's my last step to complete my first ruby application.
I am excited and looking forward to your help! :)
In your console run rake db:migrate
Make sure you in the project directory
You used devise generator to prepare migration for your User model. Your model was already in place before and already had an email column. Devise-generated migration tries to create the same column and, expectedly, fails, that's the reason for the error you're seeing:
PG::DuplicateColumn: ERROR: column "email" of relation "users" already exists
To fix that just open your devise-generated migration and remove the line which looks something like this:
t.string :email...
Then run rake db:migrate.
UPDATE
Since your database seems to be out of sync with your migrations it might be advisable to recreate it from scratch.
Run
rake db:drop db:create db:migrate
Note that all database data will be destroyed.
First I ran: rails generate model User name:string email:string and this create a migration. Later I did a db:migrate and I get this error:
bundle exec rake db:migrate
== 20150728195629 CreateUsers: migrating ======================================
-- create_table(:users)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "users" already exists.....
When you generate model the table user is created but then when you rake db:migrate it tries to create it again.
I'm confused! Am I doing something wrong?
https://www.railstutorial.org/book/modeling_users#code-generate_user_model
just run into the console
rails console
and type in
ActiveRecord::Migration.drop_table(:users)
and then
exit the console and
rake db:migrate
You must have created a table as Marsatomic said. Run
bundle exec rake db:migrate:status
And look at your migration history to see where you created it. If you see it, you can roll back your migrations past that table, delete the migration file that created it, and then re-run your migrations. If you don't see it anywhere, you must have created the table without a migration. At that point you should do as Marsatomic instructed in his comment above.
you can use 'db:reset', it == 'db:drop db:create db:migrate'
just run into the console
rails db:reset
just run into the console
bundle exec rails db:migrate
I am trying to use the social network gem inkwell.
I am following the simple instructions here .
I have created a Post, User, Category and Community model using rails generate model
I then configure the models and run
$ rake inkwell:install:migrations
$ rake db:migrate
but end up with the error
SQLite3::SQLException: no such table: posts:
There is a migrate file for this model and I always thought that once you'd created the model then following a migrate it would create a table so I'm a bit confused. Certainly when I rails console Post.all there is no table so this migration isn't happening.
As it is said in the instructions you have to create before the model for users and posts.
So if we assume you have just created the models with a generator, run the migrations for them:
$ rake db:migrate
Creating so the required tables and then:
$ rake inkwell:install:migrations
$ rake db:migrate
Did you try a rake db:reset?
Or delete the database file and:
rake db:create
rake db:migrate
I am learning Ruby on Rails, an am getting an error creating the database. I run the following command from console:
rake db:create db:migrate db:seed
And get:
== 20140328232600 AddAuthLevelToUser: migrating ===============================
-- add_column(:users, :auth_level, :Integer)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "users" does not exist
I looked at the error message on the webpage, and tried running:
'bin/rake db:migrate RAILS_ENV=development'
As suggested, but had little luck.
The project I am working on had been started by another team of developers, so I pulled it off git... Any suggestions?
Cheers
As per the error, you are running the migration AddAuthLevelToUser when you don't even have users table in database.
Firstly, verify if you have migration for users table, if not then create one.
If yes, then check that migration for users table has a VERSION NUMBER lower than that of AddAuthLevelToUser. Fix it and run the migrations.
Try running just rake db:create at first. What database is your project using? Work this out and use the database client to connect to the database on your local system, and verify whether the users table exists on the database. It's possible that the db:create job is not correctly creating all the tables necessary for the database schema.
I am trying to run the command on Heroku
Heroku run rake db:migrate
but I get the error:
Migrating to AddNameToUsers (20130320002032)
== AddNameToUsers: migrating =================================================
-- add_column(:users, :name, :string)
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::Error: ERROR: relation "users" does not exist
: ALTER TABLE "users" ADD COLUMN "name" character varying(255)
This might have to do with the fact that I had some issues with the migration files on my local server. I managed to work around it and had to delete one of the files, but I worry that I might have deleted something I need that hadn't been migrated to heroku's database?
my github for the account is https://github.com/jeremybelcher/omrails
Any help is appreciated
Your previous migrations are missing.
You can do:
rake db:create
rake db:schema:load
rake db:migrate
Which will recreate your database based on your schema.rb file.