Heroku pulls a duplicate column error in a rake db:migrate - ruby-on-rails

After pushing my master branch to heroku from git, I'm now trying to migrate my db file to heroku also but I came across this error
clydiscope$ heroku run rake db:migrate
Running `rake db:migrate` attached to terminal... up, run.6472
Migrating to DeviseCreateUsers (20141203201816)
== 20141203201816 DeviseCreateUsers: migrating ================================
-- create_table(:users)
-> 0.3488s
-- add_index(:users, :email, {:unique=>true})
-> 0.0146s
-- add_index(:users, :reset_password_token, {:unique=>true})
-> 0.0143s
== 20141203201816 DeviseCreateUsers: migrated (0.3782s) =======================
Migrating to AddNameToUsers (20141206140057)
== 20141206140057 AddNameToUsers: migrating ===================================
-- add_column(:users, :name, :string)
PG::DuplicateColumn: ERROR: column "name" of relation "users" already exists
: ALTER TABLE "users" ADD COLUMN "name" character varying(255)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::DuplicateColumn: ERROR: column "name" of relation "users" already exists
: ALTER TABLE "users" ADD COLUMN "name" character
/app/db/migrate/20141206140057_add_name_to_users.rb:3:in `up'
Apparently, there's a duplicate column that I wasn't aware of...
I was continuously migrating in the development phase and it seemed to work up to this point. How can I change ,my db now so that heroku accepts it?
Here's the code from the last line.
class AddNameToUsers < ActiveRecord::Migration
def up
add_column :users, :name, :string
end
def down
remove_column :users, :name
end
end

Probably just deleting the migration file 20141206140057_add_name_to_users.rb will solve the problem (of course, after committing and push to Heroku).

Related

Rails postgres migration trouble

rails 6.0.0 postgresql 11.5
rails db:migrate runs successfully, but there is no required column in users table.
rails db:rollback also runs successfully even without that column.
I have only one database and config files have no mistakes.
Tried to reinstall rails, postgresql and use new database dump.
class AddCountryToUsers < ActiveRecord::Migration[6.0]
def change
add_column :users, :country, :string
end
end
migration result:
== 20191105100235 AddCountryToUsers: migrating ============================
-- add_column(:users, :country, :string)
-> 0.0037s
== 20191105100235 AddCountryToUsers: migrated (0.0038s) ===================
rollback result:
== 20191105100235 AddCountryToUsers: reverting ============================
-- remove_column(:users, :country, :string)
-> 0.0027s
== 20191105100235 AddCountryToUsers: reverted (0.0028s) ===================
The most interesting thing is that manual execution of sql command creates column successfully...

Deployment issues after changing schema tables in ROR Heroku

After changing my GuideIndustry model to ConnectIndustry through find and replace, I have been getting the error below on the release log preventing me from deploying my rails app onto heroku. Is there anyway to have rails override all the files heroku is using? My local copy of all the files and the schema are working just fine. I have tried using the heroku pg:reset and then heroku run rake db:migrate.
== 20190215035620 AddArticleIdToConnectIndustry: migrating ====================
-- add_column(:connect_industries, :article_id, :integer)
(2.4ms) ALTER TABLE "connect_industries" ADD "article_id" integer
(1.0ms) ROLLBACK
(1.2ms) SELECT pg_advisory_unlock(3329669382293610510)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "connect_industries" does not exist
Update:
Through find and replace and a manual file name change, i edited the create GuideIndustry table migration to be as follows:
class CreateConnectIndustries < ActiveRecord::Migration[5.1]
def change
create_table :connect_industries do |t|
t.integer :guide_id
t.integer :industry_id
t.timestamps
end
end
end

rake aborted database will not migrate

I created models, views, and controllers for 'startups' each individually (without scaffolding). I have a file db>migrate>'201..._create_startups.rb' with the code below:
class CreateStartups < ActiveRecord::Migration
def change
create_table :startups do |t|
t.string :name
t.string :location
t.string :description
t.timestamps null: false
end
end
end
I ran "bundle exec rake db:migrate" and I get this response:
== 20141126011749 CreateStartups: migrating ===================================
-- create_table(:startups)
-> 0.0155s
== 20141126011749 CreateStartups: migrated (0.0159s) ==========================
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
wrong number of arguments (1 for 0)/Users/kevinmircovich/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract_adapter.rb:271:in `initialize'
Once I run my local server and go to my browser to view my app, I have the message below:
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
Extracted source (around line #393):
392 def check_pending!(connection = Base.connection)
393 raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration?>.>(connection)
394 end
395
396 def load_schema_if_pending!
I ran "bin/rake db:migrate RAILS_ENV=development" and had the same error as I did when I ran "bundle exec rake db:migrate":
wrong number of arguments (1 for 0)
no need to "null: false" on timestamps: it is not users' input: those are set by the active model itself, so you can remove the argument.
In Rails migration t.timestamp macro adds two columns, created_at and updated_at. These special columns are automatically managed by Active Record if they exist.
It will automatically update when new recored created & updated.
Please remove null:false argument from t.timestamp.
class CreateStartups < ActiveRecord::Migration
def change
create_table :startups do |t|
t.string :name
t.string :location
t.string :description
t.timestamps
end
end
end
I received a similar error when running rake:db migrate. To resolve my issue I ran rake:db drop to drop my database since I was in dev mode with no production database. Then I recreate the database with rake db:create after which i ran rake db:migrate successfully.
Error running rake db:migrate
ActiveRecord::PendingMigrationError
Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue.
Resolved using:
rake db:drop - this will wipe the data out of your database
rake db:create
rake db:migrate

heroku migration fails

In heroku I ran rake db:migrate and got the following error
== AlterBodyForDocuments: migrating ==========================================
-- change_column(:documents, :body, :mediumtext)
rake aborted!
An error has occurred, this and all later migrations canceled:
PGError: ERROR: type "mediumtext" does not exist
: ALTER TABLE "documents" ALTER COLUMN "body" TYPE mediumtext
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
this is my AlterBodyForDocuments migration:
class AlterBodyForDocuments < ActiveRecord::Migration
def change
change_column :documents, :body, :mediumtext
end
end
I think mediumtext is a mysql thing
heroku uses postgresql
use :text instead

Problems running migrations against users table with Postgresql

I am trying to update some default values for new columns set in a migration. However I am getting a Postgres error whenever I try to do anything with the records of users table (except modify its structure). I am using Rails 3.0.7, ruby 1.9.2 and the pg gem version 0.11.0
Here is the migration:
def self.up
add_column :users, :state_machine, :string
add_column :users, :wizard_steps_completed, :integer, :default => "1"
add_column :users, :activated_at, :datetime
User.reset_column_information
User.all.each do |u|
u.update_attributes(:state_machine => "activated", :wizard_steps_completed => 3, :activated_at => u.created_at)
end
end
The columns are added with no problems. however the changes to existing records all fail with the following error:
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
== AddUserSignupInfo: migrating ==============================================
rake aborted!
An error has occurred, this and all later migrations canceled:
PGError: ERROR: current transaction is aborted, commands ignored until end of transaction block
: SELECT COUNT(*)
FROM pg_tables
WHERE tablename = 'users'
If I attempt to update any orecord it seems to work, I can only make structural changes...
Any ideas?
Turn on postgres logging (Configured in /var/lib/pgsql/data/postgresql.conf and grep for "ERROR REPORTING AND LOGGING"). Or you might want to take the SQL and run it yourself to see what error happens. It could be a constraint thats failing because of your update.

Resources