1

I've a Postgresql database created in a rails app and I want to add default values.

I created a migration

class AddCityStateDefaults < ActiveRecord::Migration
  def change
    change_table :addresses do |t|
      t.change_default :city, default: "Los Angeles"
      t.change_default :state, default: "CA"
    end
  end
end

But this results in "---:default: Los Angeles" instead of just "Los Angeles"

Just to be sure, the two columns I'm trying to change are named city and state and they are type character varying. I created the migration via bin/rails generate migration AddCityStateDefaults and then edited the migration. I didn't think I could write the changes in the original creation (I know it can be done, but was more complex for me. I'll get to there eventually.)

I new to this, in fact the first migration I ever tried to create other than minor modifications of an existing one.

Thanks. I know it's a small change in syntax, but it was challenging to get to here.

PS. How should I have done it and is changing it now different?

1 Answer 1

1

You should pass just a value, not a hash

class AddCityStateDefaults < ActiveRecord::Migration
  def change
    change_table :addresses do |t|
      t.change_default :city, "Los Angeles"
      t.change_default :state, "CA"
    end
  end
end

http://apidock.com/rails/ActiveRecord/ConnectionAdapters/Table/change_default

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Kind of obvious. I've already stated that it's a change in default. Learning. The next ones should be easier. Simple is better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.