I have a small problem that i am facing.
When i started the project i used scaffold and defined due_date field as Date
now i want to do some date calculations. and i need to change the due_date field to Datetime . Can sm1 help me with this
I know how to add new fields to table and delete but i am stuck at changing the attribute of already existing Model.
I have tried everything. Please let me know if there is any special code i can run in terminal to edit the attribute and create migration file.
P.s- Someone told me changing the schema file is bad. so i cant edit it directly.
first generate the migration
rails g migration change_date_format_in_my_table
then inside your migration file add
def up
change_column :my_table, :my_column, :datetime
end
def down
change_column :my_table, :my_column, :date
end
run your migration and its done. :)
Related
I’ve started learning Ruby on Rails recently. I did a blogger tutorial. I want to add one more field archive to my database, but not sure is it possible just to write manually and which command to call?
Here is my database where I want to add code:
class CreateArticles < ActiveRecord::Migration[6.1]
def change
create_table :articles do |t|
t.string :title
t.text :body
t.timestamps
end
end
end
And I want to add a new field archive which is boolean and by default false?
Also is it okay to add in this schema new field or is it better in some other?
1.Run the migration from command line to add the new column
$ rails g migration add_column_archive_to_articles archive:boolean
The above command will create a new migration file in your db/migrate folder.
2.As of now there's no option to add default value to a column, which can be defined through terminal. Set the new column value to true/false by editing the new migration file created. Your migration file will look like this.
class AddColumnArchiveToArticles < ActiveRecord::Migration
def change
add_column :articles, :archive, :boolean, default: false
end
end
3.Then do
$ rails db:migrate
Migrations should never be changed afterwords. That is why they are migrations, and not a static schema definitions.
Just generate a new migration using rails g migration AddArchiveToArticles and then check the rails documentation for add_column to see how you can alter a table to add a column. It also supports default values :)
I have added new migration to change column type.
It worked well on my local.
But my team member have had the issue in migration since that column was not in migration.
It seems I had added that column manually in database.
To run migration without any issues for us(me and other team members), I want to add migration to create new column before the migration to change column type.
What is the way to do this?
Migration files have names like 20190616110000_create_foo.rb and are applied in the native alphabetical order.
Simply rename files changing the timestamps according to the order you need.
Another more proper way is in your migration (the change column type) you should add a condition to check if the column is there or not.
If it is already there (your local) then just change the column type. If it is not there (your colleagues local) then create a column with your desired type
if column_exists? :table_name, :column_name
change_column :table_name, :column_name, :type
else
add_column :table_name, :column_name, :type
end
Hello I am about to attempt to run a rails migration on a database of skills that has a :title and :description. I need to remove the description field and I assume it will look something like this:
rails migration remove_column :skills, :description
I am running it by you pros before I attempt it and end up breaking something on accident. Please let me know if I have the right idea about removing the description field from the database. Thanks a lot!
If skills is the name of your table and description is the name of the column you want to remove, you can type rails g migration RemoveDescriptionFromSkills in your terminal. This will generate a migration file with the name [timestamp]_remove_description_from_skills.rb, located in db/migrate. Edit this file so that it contains the following:
class RemoveDescriptionFromSkills < ActiveRecord::Migration
def change
remove_column :skills, :description
end
end
Then type rake db:migrate in your terminal, and the column will be removed.
For more information, check out this helpful Stack Overflow post: https://stackoverflow.com/a/1992045/3723769.
Note: this answer is intended to describe how to perform the migration. As a safety measure, you should do what Michael Durrant advises before migrating: https://stackoverflow.com/a/25006727/3723769.
Here's some of the things that some to mind:-
check the existing values to see if there's any data you want
see if any indexes exist that should also be dropped.
search the application for that field name
see if there's an existing rails migration that you can use to DOWN the change
Finally, I would consider creating a change migration as normal, i.e. one that actaully adds the field and then I would run it using the down syntax to remove the field.
I have migration (created way back) that i need to change, it's the below:
20130923000732_create_questions.rb
I need to change
t.string to --> t.text
how can i achieve this ?
I read along that i can create a new migration renaming the column, but i did not quite understand it.
If 20130923000732_create_questions.rb migration is your last migration you can rollback with:
rake db:rollback
Otherwise you can simply down your specific migration with VERSION:
rake db:migrate:down VERSION=20130923000732
After rollback your migration, change your migration file and migrate again.
Your app is in development yet, just open that migration in edtor, change it to text and run all your migrations again.
Or write a migration that will update that field type.
First in you terminal:
rails g migration change_column_type_in_questions
Then in your migration file:
class ChangeColumnTypeInQuestions < ActiveRecord::Migration
def change
change_column :questions, :body, :text
end
end
Migration will look for table questions and will update column body type without loosing data.
Run rails generate migration change_string_to_text_in_questions then a new migration file will be created, with
def change
end
method, now insert, change_column :table_name, :column_name, :type now your migration file should look like this,
def change
change_column :table_name, :column_name,:type
end
After this, Save the Changes and run db:migrate
Is it possible to rename a column using a command like:
script/generate migration AddColumnToTable column:type
? Thanks.
Rails does have a migration command on the ActiveRecord ConnectionAdapter called rename_column. You can generate a migration and then write the code yourself. example (MySQL):
script/generate migration rename_my_column_by_hand
Then edit the file it creates:
class RenameMyColumnByHand < ActiveRecord::Migration
def self.up
rename_column :my_table, :old_name, :new_name
end
def self.down
rename_column :my_table, :new_name, :old_name
end
end
It executes SQL like:
ALTER TABLE my_table CHANGE old_name new_name BIGINT;
Note This only renames the column, it won't rename any references you have to it on other tables.
Great question. The answer is, unfortunately, no. See Rails 2.3.5 source code:
lib/rails_generator/generators/components/migration/migration_generator.rb
The only keywords that are recognized by the migration generator are add, remove, and to/from.
I use a bit of trickery here. Say I want to change column foo to bar.
Create a migration with the following steps
Add a temporary column temp_foo
Update all records, saving foo's value in temp_foo
Add a bar column
Update all records, saving temp_foo's value in bar
Drop column foo
Drop column temp_foo
This is ex-tre-me-ly brittle. If one step fails, you might loose data..