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 :)
Related
I know how to add default value in a migration file already created. i.e.,
`rails generate migration AddTestColumnToTesttable test_status:boolean` to create it.
It will generate this migration:
class AddTestColumnToTable < ActiveRecord::Migration
def change
add_column :table, :test_status, :boolean, :default => true
end
end
But, Can we add the default value through rails g migration command itself?
No, it can't be done from the command line, you need to change it in the migration file
add_column :table, :test_status, :boolean, :default => true
Hope that helps!
**Rails 4.X +**
Still now, as there is no option to add new column to a table with default value defined through terminal in rails migration,
The following steps to be followed to add a new column to an existing table with default value true or false.
1. Run the migration from command line to add the new column
$ rails generate migration add_columnname_to_tablename columnname:boolean
The above command will add a new column in your table.
2. Set the new column value to TRUE/FALSE by editing the new migration file created.
class AddColumnnameToTablename < ActiveRecord::Migration
def change
add_column :tablename, :columnname, :boolean, default: false
end
end
**3. To make the changes into your application database table, run the following command in terminal**
$ rake db:migrate
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
I'm getting an error:
SQLite3::SQLException: no such column: ideas.list_id:
SELECT "ideas".* FROM "ideas"
WHERE "ideas"."list_id" = 2
But I added
t.integer :list_id
to my db migration file:
class CreateIdeas < ActiveRecord::Migration
def change
create_table :ideas do |t|
t.string :name
t.text :description
t.string :picture
t.timestamps
end
add_foreign_key :ideas, :lists
end
end
which gave me this:
class CreateIdeas < ActiveRecord::Migration
def change
create_table :ideas do |t|
t.string :name
t.text :description
t.string :picture
t.integer :list_id
t.timestamps
end
add_foreign_key :ideas, :lists
end
end
and then I typed
rake db:migrate
Any idea why I would be getting an error saying there's no column? I'm still new to RoRs. Do I have to add a column some other way?
Thanks
As Speransky suggested, you should never modify old migration files. Rather you should create a new migration that adds the desired column. For instance, in this case you would run the following command in your app to create the new migration:
rails generate migration AddListIdColumnToIdeas list_id:integer
And Rails would generate the migration file automatically and the only thing left to do is run rake db:migrate.
If you insist on modifying the old migration file, you can add the column as you did and run the following:
rake db:drop
rake db:create
rake db:migrate
Which will destroy your current database, create a new one and run all the migrations (which will include your new column).
If you want to add a new column to an exist database, you should use rails generate migration. So you can try rails generate migration add_list_id_to_ideas list_id:integer and then use rake db:migrate to commit this change.
You should not add new rows to old migrations. Migration is a step of building database. And number of last executed migration is stored in schema, and it will not be run or redone if you use will use rake db:migrate. If you run the migration with creating the table before, then you should create new migration where you may use add_column method.
migration file name has the datetime encoded in its name so rails run this migration one and do not run it again unless you do a rollback
and here come the magic of migration to build you db with small steps so no need to update a migration after run rake db:migrate , you should make a new migration to do the change you want to your db schema
and remember to
remove the added line form the old migration file as it might raise errors if you decided to rollback this migration
Rails 4.0 easy way to add single or multiple column
https://gist.github.com/pyk/8569812
You can also do this ..
rails g migration add_column_to_users list_id:string
then rake db:migrate
also add :list_id attribute in your user controller ;
for more detail check out http://guides.rubyonrails.org/active_record_migrations.html
If you already have files in your migrate folder, you could just add column you want there(just type the code), delete development.sqlite or whatever represents your db file, and run rake db:migrate.
It will then create a new sqlite file with new column in table, and you can check it in schema.rb
So, basically, everything you did seems good, except you didn't delete your database file.
Doing this seems the easiest for me, all though you will lose all the files in your database. If you're just testing and developing Rails app, this works.
Can anyone comment if there is something wrong with this approach, besides what i wrote?
Edit: I actually found an answer about that here
Editing Existing Rails Migrations is a good idea?
I happened to create a Query model in Rails and recently found out that this is one of the reserved words now..
I renamed the table using a new migration file and renamed all the files that were created (name of new model - Plot)
Question: is it OK to rename the original migration file (20111228212521_create_queries.rb) to 20111228212521_create_plots.rb)
and everything inside the old file:
class CreateQueries < ActiveRecord::Migration
def change
create_table :queries do |t|
t.string :name
t.text :content
t.timestamps
end
end
end
to
class CreatePlots < ActiveRecord::Migration
def change
create_table :plots do |t|
t.string :name
t.text :content
t.timestamps
end
end
end
??
I just don't want too many migration files and also worried that there may be some errors when I switch to production..
You can change the migration file name, but you have to perform a few steps:
rake db:rollback to the point that queries table is rolled back.
Now change the name of migration file, also the contents.
Change the name of any Model that may use the table.
rake db:migrate
The short answer is to just make another migration file.
Migration files are meant to keep track of each and every change to the database. So, you're encouraged to make small one-off changes in a separate file. I can't speak for your situation, but in my situation, when I make a mistake like this, I simply create a new migration file and don't check the old migration file into source control. This way the errant changes are only on my local db and don't get into prod/dev/staging.
Aside from rolling back, and especially useful for when you need to rename a migration from early on in production you can now in Rails 4 create a new migration to rename it.
$rails generate migration RenamesFooBarr
and then in the method of the new migration add
rename_table :old_migration_name, :new_migration name
like this:
class RenamesFooBar < ActiveRecord::Migration
def change
rename_table :old_foo_bar_name, :new_foo_bar_name
end
end
This will effectively take care of all the indexes as well in the up and down since ActiveRecord recognizes the rename_table
source: http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
Simply, do like this
Rollback the migration for that queries. i.e rake db:rollback
Change migration file, class name and the content of it.
Finally, Migrate the database. i.e rake db:migrate
In Rails 3,
I created a table with a migration, then added a column with a migration which creates a has_many, belongs_to relationship....
I then ran rake db:migrate
I'd like to now add an Index because I forgot to add it before I can migrate. can I add that to one of the existing migration files (the create table one) or do I need to create a new migration for adding an index to an existing table?
Thanks
I usually create a new migration if I forget something like that (especially if I have already migrated in production). But if you are still in development, then you can alter your last migration file and use the redo rake command:
rake db:migrate:redo
There is also:
rake db:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n).
Run this command to view all the different rake tasks:
rake -T db
Here is the section in the Rails Guide that talks about it:
http://guides.rubyonrails.org/migrations.html#rolling-back
If you would like to add your index without losing the data, you must create a new migration to add an index. Assuming your model is called Widget and the foreign model is called Zidget;
rails generate migration AddIndexToWidgets
and inside your new migration file at db/migrate/xxxxxxxxxx_add_index_to_widgets
class AddIndexToWidgets < ActiveRecord::Migration
def self.up
change_table :widgets do |t|
t.index :zidget_id # add ':unique => true' option if necessary
end
end
def self.down
change_table :widgets do |t|
t.remove_index :zidget_id
end
end
end
and then rake db:migrate as usual and voilà, you have your indexed column.
Update: If adding an index is all you're doing, there is a more concise way to write the same thing. There is no difference regarding the results. It's just that the former syntax is meant to DRY your code if you have more than one change for your table.
class AddIndexToWidgets < ActiveRecord::Migration
def self.up
add_index :widgets, :zidget_id # add ':unique => true' option if necessary
end
def self.down
remove_index :widgets, :column => :zidget_id
end
end