I'm attempting to add password verification to my application. I've been following the directions on the Devise Wiki but when I attempt the migration I get the following error:
== 20150127183211 AddConfirmableToDevise: migrating ===========================
-- add_column(:users, :confirmation_token, :string)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::DuplicateColumn: ERROR: column "confirmation_token" of relation "users" already exists
: ALTER TABLE "users" ADD "confirmation_token" character varying/Users/esteban/Code/rails/ComicBookDB/db/migrate/20150127183211_add_confirmable_to_devise.rb:4:in `up'
ActiveRecord::StatementInvalid: PG::DuplicateColumn: ERROR: column "confirmation_token" of relation "users" already exists
: ALTER TABLE "users" ADD "confirmation_token" character varying
/Users/esteban/Code/rails/ComicBookDB/db/migrate/20150127183211_add_confirmable_to_devise.rb:4:in `up'
PG::DuplicateColumn: ERROR: column "confirmation_token" of relation "users" already exists
/Users/esteban/Code/rails/ComicBookDB/db/migrate/20150127183211_add_confirmable_to_devise.rb:4:in `up'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
However, I do not see confirmation_token reflected in schema.rb. Perhaps I should reset my database? If so, what is the easiest way to do so? Any help would be appreciated.
It depends on how you installed your current database.
If you made it with rake db:create/rake db:migrate - there should be a migration with corresponding fields(maybe removed/modified, they can not magically appear).
If you setup your db using someones dump - it explains how fields appeared in db, but not in schema.
If you are in development mode and have no valuable information in database, easiest way is to re-create your db from scratch:
rake db:drop:all
rake db:create:all
rake db:migrate
If you have valuable information or your migrations don't fully represent current state of db(maybe you removed some of the migrations or use dumps in team to share information), just skip the step with fields creation in instructions since you already have them.
Related
I'm having some DB issues with rails. When I run rails db:migrate I get the following error:
add_column(:locations, :user_id, :integer)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: no such table: locations: ALTER TABLE "locations" ADD "user_id" integer
The problem seems to be that I have a migration that is trying to add :user_id to a table :locations but this table does not exist because I deleted it and it is therefore not in the schema. How do I solve this? I always thought it was a bad idea to delete migrations.
It depends. Was locations created and deleted by migrations? If so, is the failing migration, in time terms, between those migrations?. If the answer is yes then you should not delete it.
In the other hand, if you deleted locations by hand or by migration before the failing migration then remove it, since it makes no sense to your schema.
I'm trying to install activeadmin on rails with devise.
I added gemfile and ran
rake db:migrate
and i met these errors.
== 20170821153121 DeviseCreateAdminUsers: migrating ===========================
-- create_table(:admin_users)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "admin_users" already exists: CREATE TABLE "admin_users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "email" varchar DEFAULT '' NOT NULL, "encrypted_password" varchar DEFAULT '' NOT NULL, "reset_password_token" varchar, "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, "last_sign_in_ip" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
/Users/admin/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `initialize'
/Users/admin/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `new'
/Users/admin/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `prepare'
/Users/admin/.rvm/gems/ruby-2.4.1/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:137:in `execute'
...
Well the error is fairly self explanatory, your database already has a table with the same name Devise/Active Admin wants to use and table names must be unique.
Did you already at some point already use/try the activeadmin gem? If you already created the tables for it, trying to create and execute new migrations will fail, but you should be able to either just use those, or delete them and make fresh ones.
Or does your application just happen to have a AdminUser model for its own use? If it does one of them has to change.
You should be able to see either case in your db/schema.rb, or in the old migrations in db/migrations/. If you tried deleting/reverting a migration by just deleting its migration without script without rolling back first (either rails db:rollback or a DB backup), you may also have old tables actually in the DB, which you may want to manually delete.
If you do need to rename the Active Admin tables, the advice given in Github Database tables name prefix #4960 is to manually edit the migration and use self.table_name= in the models to tell them the new names.
Well, the error it self repeat that you have already the same table called "admin_users".
Please run the command on rails console to see your already created tables like this way:
ActiveRecord::Base.connection.tables
If you would find same table in the list then you need to delete that table manually or another way is just drop your db and recreate it after that you can able to run your new migration.
I have a local PSQL database setup and am having issues getting rake db:migrate to function properly. For instance, my database has already had its high_school column name changed to high_school_name, but rake db:migrate is failing, and the status reveals this:
up 20130307043554 Adding seo tables
up 20130307185401 Create admin notes
up 20130307185402 Move admin notes to comments
up 20130308160956 Add active flad to users
up 20130308214928 Add column public to users table
up 20130325203837 Add duration to videos
up 20130326171803 Update duration of videos
down 20130410145000 Fix high school name
up 20130410145028 Add high school id to users
up 20130410161705 Convert units for stats
up 20130410164209 ********** NO FILE **********
up 20130416142844 Add column coach id to users
Why is a migration in the middle of the migration order failing/being read as "down"? Here's the error:
Migrating to AddingSeoTables (20130307043554)
Migrating to CreateAdminNotes (20130307185401)
Migrating to MoveAdminNotesToComments (20130307185402)
Migrating to AddActiveFladToUsers (20130308160956)
Migrating to AddColumnPublicToUsersTable (20130308214928)
Migrating to AddDurationToVideos (20130325203837)
Migrating to UpdateDurationOfVideos (20130326171803)
Migrating to FixHighSchoolName (20130410145000)
(0.1ms) BEGIN
== FixHighSchoolName: migrating ==============================================
-- rename_column(:users, :high_school, :high_school_name)
(0.4ms) ALTER TABLE "users" RENAME COLUMN "high_school" TO "high_school_name"
PG::Error: ERROR: column "high_school" does not exist
: ALTER TABLE "users" RENAME COLUMN "high_school" TO "high_school_name"
(0.1ms) ROLLBACK
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::Error: ERROR: column "high_school" does not exist
: ALTER TABLE "users" RENAME COLUMN "high_school" TO "high_school_name"
Obviously, there is a schema issue or something. Schema is tracked via the remote branch though, so I don't know where the issue lies.
I suspect that the down was not able to handle the column rename change correctly, unless just adding or removing columns.
One option you may wish to consider is actually changing the name with SQL. For instance if a migration to change high_school to high_school_name is failing and the database itself currently has high_school_name, rename that (in SQL) to high_school and then try and run the migration. This may be one option for you.
Add resque nil to at the end of the row where column is renaming
I've ended up with 9 migrations effectively duplicated. (I think this is because I installed/updated Gems and/or pulled in their migrations on both my dev and production machines, but am not entirely sure at this stage.)
I moved out one set of the duplicated 9 from the rails directories on the production server, but now that I want to db:migrate on production in order to run another migration, I'm getting:
$ bundle exec rake db:migrate RAILS_ENV=production
[DEPRECATION WARNING] Nested I18n namespace lookup under "activerecord.attributes.checkout" is no longer supported
== CreatePages: migrating ====================================================
-- create_table(:pages)
rake aborted!
An error has occurred, all later migrations canceled:
Mysql2::Error: Table 'pages' already exists: CREATE TABLE `pages` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `title` varchar(255), `body` text, `slug` varchar(255), `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB
This is because the migrations have effectively already been run.
I'd rather avoid doing db:migrate:down and db:migrate:up for each one - I think this will mean data in the production database is lost. (A couple of static pages in Spree in this case.)
Is there a way I can tell this installation of Rails to forget all outstanding migrations, effectively marking all outstanding migrations as done?
I solved it like this:
Go to the conflicting migration file.
Erase the content and save it.
Run rake db:migrate
Ctrl+Z the file to the previous state.
This was an special case, because I had copied the DB from another app, and I had conflicting migrations, and stuff.
You can add the timestamps of the migrations to the schema_migrations table. However why is that table missing from the database or missing the rows it needs?
It is likely to be the case that this particular migration has got half way through and failed, thus when you are trying to run it again the first part of the migration will not work as it has been done previously. This is a limitation of MySQL as it can't rollback migration changes that fail part of the way through. Postgres on the other hand can rollback structural changes to the database thus avoiding this issue.
By default rake db:migrate runs all the pending migrations. So, in order to get your migrations right.. for the sake ..comment out those migrations and afterwards revert those back to normal. It will ensure that you are ok in future migrations.
You can also, in a Rails console, use the ActiveRecord::SchemaMigration model to add the timestamps into the schema_migrations table.
ActiveRecord::SchemaMigration.create! version: '20210724133241'
I'm following the RailsTutorial.org tutorial, section 7.3 and trying to do a db migration using rake to add a password column to an existing database. What appears to be happening is that rails is re-running a previous migration file and trying to add table Users (which exists already) rather than the most recent migration file and add a password column. Any help would be appreciated!
Here is the code I ran to generate the migration file:
$ rails generate migration add_password_to_users encrypted_password:string
Then I ran rake db:migrate and got the following error:
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, "name" varchar(255), "email" varchar(255), "created_at" datetime, "updated_at" datetime)
Rails will run any migration whose number is not in the schema_migrations table in the relevant database. So, if it doesn't have the number for the migration to create the users table, it will run that migration, which will blow up as you have seen if the table happens to be there already.
I'm not familiar with the tutorial you're talking about - can you take me through what you have done already with your migrations?
It appears there was a problem with one of the other migration files -- I moved that file out of the migrate folder and re-ran rake and it worked.