rails-migrations

Rails migration to change column type from text to json (Postgresql)

廉价感情. 提交于 2020-08-06 11:27:42
问题 I've been trying unsuccessfully to change a column type in my Postgres database from text to json. Here's what I've tried... class ChangeNotesTypeInPlaces < ActiveRecord::Migration[5.0] def up execute 'ALTER TABLE places ALTER COLUMN notes TYPE json USING (notes::json)' end def down execute 'ALTER TABLE places ALTER COLUMN notes TYPE text USING (notes::text)' end end Also... class ChangeNotesTypeInPlaces < ActiveRecord::Migration[5.0] def up change_column :places, :notes, 'json USING CAST

Rails migration to change column type from text to json (Postgresql)

这一生的挚爱 提交于 2020-08-06 11:27:30
问题 I've been trying unsuccessfully to change a column type in my Postgres database from text to json. Here's what I've tried... class ChangeNotesTypeInPlaces < ActiveRecord::Migration[5.0] def up execute 'ALTER TABLE places ALTER COLUMN notes TYPE json USING (notes::json)' end def down execute 'ALTER TABLE places ALTER COLUMN notes TYPE text USING (notes::text)' end end Also... class ChangeNotesTypeInPlaces < ActiveRecord::Migration[5.0] def up change_column :places, :notes, 'json USING CAST

Specify custom index name when using add_reference

瘦欲@ 提交于 2020-07-04 20:53:29
问题 I have the following migration class LinkDoctorsAndSpecializations < ActiveRecord::Migration def up add_reference :doctors, :doctor_specialization, polymorphic: true, index: true end def down remove_reference :doctors, :doctor_specialization, polymorphic: true end end when i run rake db:migrate i am getting the error Index name 'index_doctors_on_doctor_specialization_type_and_doctor_specialization_id' on table 'doctors' is too long; the limit is 63 characters so how can i specify the index

Specify custom index name when using add_reference

随声附和 提交于 2020-07-04 20:51:09
问题 I have the following migration class LinkDoctorsAndSpecializations < ActiveRecord::Migration def up add_reference :doctors, :doctor_specialization, polymorphic: true, index: true end def down remove_reference :doctors, :doctor_specialization, polymorphic: true end end when i run rake db:migrate i am getting the error Index name 'index_doctors_on_doctor_specialization_type_and_doctor_specialization_id' on table 'doctors' is too long; the limit is 63 characters so how can i specify the index

How to define the sequence to use when creating a table in ActiveRecord migration in Ruby on Rails 5.2?

别来无恙 提交于 2020-06-26 06:26:48
问题 I need to assign a specific Postgres sequence to the ID field of my table. In the model, I tried to define the following setup which has no effect on Posgres: class MyObject < ActiveRecord::Base self.sequence_name = "global_seq" Usually, a table definition in ActiveRecord migrations start with create_table "objects", id: :serial, force: :cascade do |t| which generates a Postgres definition of column default value as default nextval('objects_id_seq'::regclass) How can I specify in the

Rails 3.2.6 & database views creation through migrations

六月ゝ 毕业季﹏ 提交于 2020-02-27 04:37:25
问题 I'm using rails 3.2.6 and I need to create a database VIEW. As usual I created a migration and I tried to achieve the goal using the execute method. Unfortunately the migration generates a table, not a view. Why? Many thanks in advance, Mauro UPDATE: I would like to have something as follows: class CreateMyView < ActiveRecord::Migration def self.up execute <<-SQL CREATE VIEW my_view AS SELECT ... SQL end def self.down execute <<-SQL DROP VIEW my_view SQL end end Unfortunately this migration

Rails 3.2.6 & database views creation through migrations

馋奶兔 提交于 2020-02-27 04:36:49
问题 I'm using rails 3.2.6 and I need to create a database VIEW. As usual I created a migration and I tried to achieve the goal using the execute method. Unfortunately the migration generates a table, not a view. Why? Many thanks in advance, Mauro UPDATE: I would like to have something as follows: class CreateMyView < ActiveRecord::Migration def self.up execute <<-SQL CREATE VIEW my_view AS SELECT ... SQL end def self.down execute <<-SQL DROP VIEW my_view SQL end end Unfortunately this migration

Add a reference column migration in Rails 5

跟風遠走 提交于 2020-02-20 06:21:09
问题 A user has many uploads. I want to add a column to the uploads table that references the user. What should the migration look like? Related question for Rails 3: Rails 3 migrations: Adding reference column? Related question for Rails 4: Add a reference column migration in Rails 4 回答1: As with prior versions of Rails, you may use the following command to create the migration: rails g migration AddUserToUploads user:references Unlike prior versions of Rails, the migration looks like: class

How to write a migration to convert an already-present association to optional?

这一生的挚爱 提交于 2020-01-30 08:12:57
问题 I have a few belongs_to migrations created&migrated but I see now that in Rails 5, the default behavior is required: true which I do not want. My question is that how can I write a simple migration to convert that to optional: true ? Can't find this particular solution anywhere. 回答1: You can use the change_column_null method for that: def change change_column_null :table, :column, true end change_column_null is reversible, if you need to rollback the value will be false (or true depending on

How to write a migration to convert an already-present association to optional?

99封情书 提交于 2020-01-30 08:12:06
问题 I have a few belongs_to migrations created&migrated but I see now that in Rails 5, the default behavior is required: true which I do not want. My question is that how can I write a simple migration to convert that to optional: true ? Can't find this particular solution anywhere. 回答1: You can use the change_column_null method for that: def change change_column_null :table, :column, true end change_column_null is reversible, if you need to rollback the value will be false (or true depending on