Using active admin gem for administration - ruby-on-rails

Iam using active admin gem for administration purpose i have included the gems
gem 'activeadmin', github: 'gregbell/active_admin'
gem 'devise'
and the commands
bundle install
rails generate active_admin:install
but when i run the rake db:migrate iam getting following error
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Table 'admin_users' already exists:
CREATE TABLE `admin_users` (`id` int(11) auto_increment PRIMARY KEY,
`email` varchar(255) DEFAULT '' NOT NULL, `encrypted_password` varchar(255)
DEFAULT '' NOT NULL, `reset_password_token` varchar(255), `reset_password_sent_at`
datetime, `remember_created_at` datetime, `sign_in_count` int(11) DEFAULT 0 NOT NULL,
`current_sign_in_at` datetime, `last_sign_in_at` datetime, `current_sign_in_ip`
varchar(255), `last_sign_in_ip` varchar(255), `created_at` datetime, `updated_at`
datetime) ENGINE=InnoDB/home/ameex/.rvm/gems/ruby-2.1.2/gems/activerecord-
4.1.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:301:in `query'

As error suggests you have an existing table named admin_users. you may have already attempted an active_admin migration, try resetting your database.
rake db:reset

Also, We can do these three steps too.
rake db:drop
rake db:create
rake db:migrate
or simply,
rake db:migrate:reset => which runs db:drop db:create db:migrate

Related

Error in bin/rake db:migrate

when I run this command bin/rake db:migrate
I get this error
== 20151020021106 CreateTodoItems: migrating ================================== -- create_table(:todo_items) rake aborted! StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "todo_items" already exists: CREATE TABLE "todo_items" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "todo_list_id" integer, "content" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) /home/youta/rails_projects/odot/db/migrate/20151020021106_create_todo_items.rb:3:in change' -e:1:in' ActiveRecord::StatementInvalid: SQLite3::SQLException: table "todo_items" already exists: CREATE TABLE "todo_items" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "todo_list_id" integer, "content" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) /home/youta/rails_projects/odot/db/migrate/20151020021106_create_todo_items.rb:3:in change' -e:1:in' SQLite3::SQLException: table "todo_items" already exists /home/youta/rails_projects/odot/db/migrate/20151020021106_create_todo_items.rb:3:in change' -e:1:in' Tasks: TOP => db:migrate (See full trace by running task with --trace)
To solve it, I tried to remove the model todo_item by using these commands
rake db:rollback
rails destroy model todo_item
rake db:drop
rake db:setup
rake db:drop RAILS_ENV=test
rake db:setup RAILS_ENV=test
Although when I tried to remake the todo_item model after removing the previous one I still get the same error!
should I reset git .. if so, how can I reset it to the previous working version
Running rake db:setup is not the same as running all migrations, but uses your schema.rb to rebuild the database, which still includes the todo_items table.
Drop the database again with rake db:drop, and run rake db:migrate to only use the migrations in your migrate directory.

Ruby-on-Rails Tutorial Trouble

I am new to both Ruby and Rails. So this may be an easy fix. I'm sorry if it is.
I recently installed Ruby-on-Rails and started following the tutorial on rubyonrails.org which shows how to make a simple blog. Everything was running fine until I got to section 5.5. I went to run db:migrate and it gave me an error.
|D:\Documents\Programs\Ruby\blog>rake db:migrate
== 20141216061542 CreateArticles: migrating ===================================
-- create_table(:articles)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "articles" already exists: CREATE TABLE "articles" ("id" INTEGER
PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "text" text, "created_at" datetime,
"updated_at"
datetime) D:/Documents/Programs/Ruby/blog/db/migrate/20141216061542_create_articles.rb:3:in
`change
'
C:in `migrate'
ActiveRecord::StatementInvalid: SQLite3::SQLException: table "articles" already exists: CREATE
TABLE
"articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "text" text,
"created_at" datetime, "updated_at" datetime)
D:/Documents/Programs/Ruby/blog/db/migrate/20141216061542_create_articles.rb:3:in `change'
C:in `migrate'
SQLite3::SQLException: table "articles" already exists
D:/Documents/Programs/Ruby/blog/db/migrate/20141216061542_create_articles.rb:3:in `change'
C:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
I fired up the server to see what it would show and it gave me this:
ActiveRecord::PendingMigrationError
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
It's been doing this ever since. I have tried starting over by deleting the project.(not entirely sure if that was a good move.) I have tried looking over the code. Nothing I have tried has given me any hints on what to do.
Is there any way to get rid of these errors?
Thank you in advance.
EDIT:
I tried to reset the database with 'rake db:reset', but it just gave me this:
|D:\Documents\Programs\Ruby\blog\app\views\articles>rake db:reset
(in D:/Documents/Programs/Ruby/blog)
Permission denied # unlink_internal - D:/Documents/Programs/Ruby/blog/db/development.sqlite3
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/fileutils.rb:1460:in `unlink'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/fileutils.rb:1460:in `block in remove_file'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/fileutils.rb:1468:in `platform_support'
...
rake aborted!
Errno::EACCES: Permission denied # unlink_internal -
D:/Documents/Programs/Ruby/blog/db/development.
sqlite3
Tasks: TOP => db:schema:load
(See full trace by running task with --trace)
I shortened it for readability.
And here is my create_articles migration file:
class CreateArticles < ActiveRecord::Migration
def change
create_table :articles do |t|
t.timestamps
end
end
end
You've already created that particular table. Try this from your terminal:
rake db:drop db:create db:migrate
Or:
rake db:reset db:migrate
So basically, you will start your database from scratch, which will avoid the current error.
Note that for new migrations, you only run the 'rake db:migrate' command otherwise your existing data will be lost.
Later on if you come across this problem in a production environment, ensure that you do 'something else' - surely you wouldn't want to sacrifice your production database data.
Well It seems obvious, you already have table articles, and you are trying to create a new one.
Two options:
comment migration with articles do rake db:migrate, uncomment for other environment (if any)
clear database and run migrations again.
Add create_articles to your question as well, could help resolving the problem.
Drop the db
rake db:drop
And Migrated it once again
rake db:migrate
You have already create articles tables. So you need to drop it and migrate it once again.

rake db:migrate error tables

I am trying to run rake db:migrate and am receiving an error in the console.
It seems as though I am creating a table that already exists, yet I don't know how to remove the old table, or reset the db to start fresh.
I don't have any users so erasing or starting from fresh won't be an issue.
create_table(:users) rake aborted! StandardError: An error has
occurred, this and all later migrations canceled:
SQLite3::SQLException: table "users" already exists: CREATE TABLE
"users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email"
varchar(255) DEFAULT '' NOT NULL, "encrypted_password" varchar(255)
DEFAULT '' NOT NULL, "reset_password_token" varchar(255),
"reset_password_sent_at" datetime, "remember_created_at" datetime,
"sign_in_count" integer DEFAULT 0 NOT NULL, "current_sign_in_at"
datetime, "last_sign_in_at" datetime, "current_sign_in_ip"
varchar(255), "last_sign_in_ip" varchar(255), "created_at" datetime,
"updated_at" datetime)
/Users/jovanhernandez/.rvm/gems/ruby-2.1.2/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:91:in
`initialize'
If you don't mind erasing data you can run
rake db:drop
rake db:create
rake db:migrate
and that should fix it. Otherwise you can for the moment comment out the part of the content causing problems in the change (or up) method in your migration and then run the migrations. After the migration is run uncomment the migration.
Doing this tricks rails into accepting that the migrations are up to date.
You can reset database by using
$ rake db:reset
Or if you want to undo your migration you can use
$ rake db:rollback
Or if you want to update your table, you may change it in your migration file from create_table to change_table
create_table :users do |t|
to
change_table(:users) do |t|
Look like you have not any users migration but you have migration to change the users table.Means you are trying to alter users table without existence of it.If you have schema file then you can load your database from there by using rake db:schema:dump command.
You already have the users table created. Looks like the migration was already run and there is no need to run it again.

Devise throws and error during migration

I am trying to use Devise in my rails app but when I get to the point to migrate my database Devise gives me this error:
rake db:migrate
== 20141016065244 AddDeviseToPatients: migrating ==============================
-- change_table(:patients)
-> 0.0127s
-- add_index(:patients, :email, {:unique=>true})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::ConstraintException: UNIQUE constraint failed: patients.email: CREATE UNIQUE INDEX "index_patients_on_email" ON "patients" ("email")
I am trying to use devise in an already started project where the "User" table is called "Patient".
This is that table:
"Patient(id: integer, name: string, loginName: string, login: integer, created_at: datetime, updated_at: datetime)"
So what simple error am I making? :)
Here's the migration file: https://gist.github.com/macmattias/1c81717dbf218d18dc72
The field email is missing?
You need to add a field email to your table patients.

Rake db:migrate error after db:reset

I am having trouble with running rake db:migrate and I get an error even after rake db:reset, and it says to run rake db:migrate and has the same error. What can I do to make it work and have the migration go through
SQLite3::SQLException: table "users" already exists: CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "username" varchar(255), "password_digest" varchar(255), "created_at" datetime, "updated_at" datetime) /Users/alextuazon/.rvm/gems/ruby-2.1.0/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:91:in `initialize'
/Users/alextuazon/.rvm/gems/ruby-2.1.0/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:91:in `new'
/Users/alextuazon/.rvm/gems/ruby-2.1.0/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:91:in `prepare'
/Users/alextuazon/.rvm/gems/ruby-2.1.0/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:134:in `execute'
/Users/alextuazon/.rvm/gems/ruby-2.1.0/gems/activerecord-4.1.2/lib/active_record/connection_adapters/sqlite3_adapter.rb:334:in `block in execute'
Do a rake db:drop to drop the tables and then rake db:migrate to create them and all the other stuff your migrations do.
EDIT: Additionally, reading Difference between rake db:migrate db:reset and db:schema:load the answers to might be helpful

Resources