기존 Rails 열에서 부울에 : default => true 추가
기존 열에 기본 부울 값을 추가하는 방법에 대한 몇 가지 질문 (즉, this )을 보았습니다 . 그래서 나는 change_column
제안을 시도했지만 제대로하지 않아야합니다.
나는 시도했다 :
$ change_column :profiles, :show_attribute, :boolean, :default => true
어떤 반환 -bash: change_column: command not found
나는 다음을 달렸다.
$ rails g change_column :profiles, :show_attribute, :boolean, :default => true
...과
$ rails change_column :profiles, :show_attribute, :boolean, :default => true
그런 다음 실행 rake db:migrate
했지만 값은 :show_attribute
그대로 유지되었습니다 nil
. 위에서 언급 한 질문에서 PostgreSQL에서는 수동으로 업데이트해야한다고 말합니다. PostgreSQL을 사용하고 있기 때문에 create_profiles
마이그레이션에 다음을 추가했습니다 .
t.boolean :show_attribute, :default => true
누군가 내가 여기서 뭘 잘못하고 있는지 말해 줄 수 있습니까?
change_column
의 메소드 ActiveRecord::Migration
이므로 콘솔에서와 같이 호출 할 수 없습니다.
이 열에 기본값을 추가하려면 새 마이그레이션을 작성하십시오.
rails g migration add_default_value_to_show_attribute
그런 다음 마이그레이션에서 생성되었습니다.
# That's the more generic way to change a column
def up
change_column :profiles, :show_attribute, :boolean, default: true
end
def down
change_column :profiles, :show_attribute, :boolean, default: nil
end
또는 더 구체적인 옵션 :
def up
change_column_default :profiles, :show_attribute, true
end
def down
change_column_default :profiles, :show_attribute, nil
end
그런 다음를 실행하십시오 rake db:migrate
.
이미 생성 된 레코드로는 아무것도 변경되지 않습니다. 그러기 위해서는를 만들 rake task
거나 rails console
모든 레코드를 업데이트해야합니다.
당신이 추가 할 때 t.boolean :show_attribute, :default => true
받는 create_profiles
마이그레이션은 아무것도하지 않은 경우, 그것은 정상입니다. 아직 실행되지 않은 마이그레이션 만 실행됩니다. 새로운 데이터베이스로 시작한 경우 기본값이 true로 설정됩니다.
허용되는 답변의 변형으로 change_column_default
마이그레이션에서이 방법을 사용할 수도 있습니다 .
def up
change_column_default :profiles, :show_attribute, true
end
def down
change_column_default :profiles, :show_attribute, nil
end
이것이 언제 작성되었는지 확실하지 않지만 현재 마이그레이션의 열에서 기본값을 추가하거나 제거하려면 다음을 사용할 수 있습니다.
change_column_null :products, :name, false
레일 5 :
change_column_default :products, :approved, from: true, to: false
http://edgeguides.rubyonrails.org/active_record_migrations.html#changing-columns
레일스 4.2 :
change_column_default :products, :approved, false
http://guides.rubyonrails.org/v4.2/active_record_migrations.html#changing-columns
Which is a neat way of avoiding looking through your migrations or schema for the column specifications.
Also, as per the doc:
default cannot be specified via command line
https://guides.rubyonrails.org/active_record_migrations.html
So there is no ready-made rails generator. As specified by above answers, you have to fill manually your migration file with the change_column_default
method.
You could create your own generator: https://guides.rubyonrails.org/generators.html
If you just made a migration, you can rollback and then make your migration again.
To rollback you can do as many steps as you want:
rake db:rollback STEP=1
Or, if you are using Rails 5.2 or newer:
rails db:rollback STEP=1
Then, you can just make the migration again:
def change
add_column :profiles, :show_attribute, :boolean, default: true
end
Don't forget to rake db:migrate
and if you are using heroku heroku run rake db:migrate
change_column :things, :price_1, :integer, default: 123, null: false
Seems to be best way to add a default to an existing column that doesn't have null: false
already.
Otherwise:
change_column :things, :price_1, :integer, default: 123
Some research I did on this:
https://gist.github.com/Dorian/417b9a0e1a4e09a558c39345d50c8c3b
참고URL : https://stackoverflow.com/questions/8627156/adding-default-true-to-boolean-in-existing-rails-column
'IT' 카테고리의 다른 글
모든 쿼리를 기록하는 MongoDB (0) | 2020.06.08 |
---|---|
Ruby에서 배열의 교차, 합집합 및 부분 집합을 얻으려면 어떻게해야합니까? (0) | 2020.06.08 |
wget을 사용하여 웹 사이트에서 HTML이 아닌 모든 파일을 다운로드하는 방법은 무엇입니까? (0) | 2020.06.08 |
탐색 모음 표시 / 숨기기 (0) | 2020.06.08 |
렌더링 문제 렌더링 중 예외 발생 : com.android.ide.common.rendering.api.LayoutlibCallback (0) | 2020.06.08 |