Change integer to float column table Rails - ruby-on-rails

I need to change t.integer :mark_up to a float How can I do this? I have tried in my terminal rails g migration change_column(:stakes, :mark_up, :float) by keep getting a syntax error near unexpected token ('

In your terminal:
rails generate migration ChangeMarkUpToFloat
and in the file that is created: db/migrate/2015xxxxxxxxxx/change_mark_up_to_float.rb
edit it to:
class ChangeMarkUpToFloat < ActiveRecord::Migration
def change
change_column :stakes, :mark_up, :float
end
end
and then back in your terminal:
rake db:migrate

You can't use rails code (change_column) in your terminal.
What you need to do is first create the migration: rails generate migration ChangeMarkUpType and then put your rails code in the file that was created.
You can read more about migrations here

Related

Rails migration not doing anything?

I created the following migration:
class AddTokenToRegionTriggers < ActiveRecord::Migration[5.0]
def change
add_column :region_triggers, :token, :string
add_index :region_triggers, :token, unique: true
end
end
There's a table called region_triggers. I tried running rake db:migrate:up VERSION=123, but it didn't do anything. The output is:
Enabling Bootsnap
Model files unchanged.
When I ran rake db:migrate:redo VERSION=123, it said No indexes found on region_triggers with the options provided..
How do I make rails add the column and the index?
Where did you get the version number from? Did you create the migration file using rails g migration ?
If so, it would have created the migration file with a version number prefixing the model name, so (for example)
rails g migration AddTokenToRegionTriggers
Would generate
20180501113025_add_token_to_region_triggers.rb
That initial numeric string is the version number. To migrate up to that version, you can do
rake db:migrate:up VERSION=2018051113025
If you have no later migrations, you could just do...
rake db:migrate

Create a field in a table using Command Prompt *Ruby on Rails

How do I create a new field in an existing table using the Ruby command prompt?
I created the model (and migrated it) but I forgot to add a field - how would I go about this?
Generate a new migration to add a new column to table.
rails g migration add_column_name_to_table_name column_name:type
This will create a migration class like below:
#config/migration/20150304121554_add_column_name_to_table_name.rb
class AddColumnNameToTableName < ActiveRecord::Migration
def change
add_column :table_name, :column_name, :type
end
end
Here, column_name, table_name and type should be your desired name and type. Than run rake db:migrate command.
There are two ways to make changes in your situation:
Roll back the last migration
Add the new field in a new migration
Undoing the last migration should only be done if you have not yet pushed the migration to a public server. Here's how to do it:
Run rake db:rollback
Add the new field to the same migration file you originally used
Run rake db:migrate
Option 2:
To add the field in a new migration:
rails g migration AddFieldNameToTableName
For example, if your field is name and your table is users, you would run:
rails g migration AddNameToUsers
This will create a new migration file whose name starts with today's date and ends with add_name_to_users.rb. Open the file and add the field using the add_column command, like this:
class AddNameToUsers < ActiveRecord::Migration
def change
add_column :users, :name, :string
end
end
Save the file, then run rake db:migrate.
I encourage you to read the Rails migrations guide to learn more.

Command to Change DB Column in Ruby on Rails

Is there a quick console command I can run to change the type I have for an object? It's currently a Ruby Date type, but I would like it to be a Ruby Time type.
I started with this scaffold command:
$ rails generate scaffold Post title:string content:text postdate:date
But wish I would have done the following:
$ rails generate scaffold Post title:string content:text postdate:time
Is there a command and can run to make the update?
Sometimes you have to write some actual code, even in Rails. Try creating migration and then using change_column method. Something like
change_column :my_table, :my_column, :new_type
You put this in your db migration file, not in shell.
If you don't want the mistake to be a permanent part of the migration set, simply migrate down (rake db:rollback), edit your migration file, and migrate back up (rake db:migrate).
Edit: To answer your question about there being a single command? Yes, there is. After editing your migration:
rake db:migrate:redo
This runs the "down" followed by the "up" in just one command.
Three steps to change the type of a column:
Step 1:
Generate a new migration file using this code:
rails g migration sample_name_change_column_type
Step 2:
Go to /db/migrate folder and edit the migration file you made. There are two different solutions.
def change
change_column(:table_name, :column_name, :new_type)
end
2.
def up
change_column :table_name, :column_name, :new_type
end
def down
change_column :table_name, :column_name, :old_type
end
Step 3:
Don't forget to do this command:
rake db:migrate
I have tested this solution for Rails 4 and it works well.

Rails DB Migration - How To Drop a Table?

I added a table that I thought I was going to need, but now no longer plan on using it. How should I remove that table?
I've already run migrations, so the table is in my database. I figure rails generate migration should be able to handle this, but I haven't figured out how yet.
I've tried:
rails generate migration drop_tablename
but that just generated an empty migration.
What is the "official" way to drop a table in Rails?
You won't always be able to simply generate the migration to already have the code you want. You can create an empty migration and then populate it with the code you need.
You can find information about how to accomplish different tasks in a migration here:
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
More specifically, you can see how to drop a table using the following approach:
drop_table :table_name
Write your migration manually. E.g. run rails g migration DropUsers.
As for the code of the migration I'm just gonna quote Maxwell Holder's post Rails Migration Checklist
BAD - running rake db:migrate and then rake db:rollback will fail
class DropUsers < ActiveRecord::Migration
def change
drop_table :users
end
end
GOOD - reveals intent that migration should not be reversible
class DropUsers < ActiveRecord::Migration
def up
drop_table :users
end
def down
fail ActiveRecord::IrreversibleMigration
end
end
BETTER - is actually reversible
class DropUsers < ActiveRecord::Migration
def change
drop_table :users do |t|
t.string :email, null: false
t.timestamps null: false
end
end
end
First generate an empty migration with any name you'd like. It's important to do it this way since it creates the appropriate date.
rails generate migration DropProductsTable
This will generate a .rb file in /db/migrate/ like 20111015185025_drop_products_table.rb
Now edit that file to look like this:
class DropProductsTable < ActiveRecord::Migration
def up
drop_table :products
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
The only thing I added was drop_table :products and raise ActiveRecord::IrreversibleMigration.
Then run rake db:migrate and it'll drop the table for you.
Warning: Do this at your own risk, as #z-atef and #nzifnab correctly point out, Rails will not be aware of these changes, your migration sequence fill fail and your schema will be different from your coworkers'. This is meant as a resource for locally tinkering with development only.
While the answers provided here work properly, I wanted something a bit more 'straightforward', I found it here: link
First enter rails console:
$rails console
Then just type:
ActiveRecord::Migration.drop_table(:table_name)
And done, worked for me!
You need to to create a new migration file using following command
rails generate migration drop_table_xyz
and write drop_table code in newly generated migration file (db/migration/xxxxxxx_drop_table_xyz) like
drop_table :tablename
Or if you wanted to drop table without migration, simply open rails console by
$ rails c
and execute following command
ActiveRecord::Base.connection.execute("drop table table_name")
or you can use more simplified command
ActiveRecord::Migration.drop_table(:table_name)
rails g migration drop_users
edit the migration
class DropUsers < ActiveRecord::Migration
def change
drop_table :users do |t|
t.string :name
t.timestamps
end
end
end
rake db:migrate
The simple and official way would be this:
rails g migration drop_tablename
Now go to your db/migrate and look for your file which contains the drop_tablename as the filename and edit it to this.
def change
drop_table :table_name
end
Then you need to run
rake db:migrate
on your console.
I wasn't able to make it work with migration script so I went ahead with this solution. Enter rails console using the terminal:
rails c
Type
ActiveRecord::Migration.drop_table(:tablename)
It works well for me. This will remove the previous table. Don't forget to run
rails db:migrate
I think, to be completely "official", you would need to create a new migration, and put drop_table in self.up. The self.down method should then contain all the code to recreate the table in full. Presumably that code could just be taken from schema.rb at the time you create the migration.
It seems a little odd, to put in code to create a table you know you aren't going to need anymore, but that would keep all the migration code complete and "official", right?
I just did this for a table I needed to drop, but honestly didn't test the "down" and not sure why I would.
you can simply drop a table from rails console.
first open the console
$ rails c
then paste this command in console
ActiveRecord::Migration.drop_table(:table_name)
replace table_name with the table you want to delete.
you can also drop table directly from the terminal. just enter in the root directory of your application and run this command
$ rails runner "Util::Table.clobber 'table_name'"
You can roll back a migration the way it is in the guide:
http://guides.rubyonrails.org/active_record_migrations.html#reverting-previous-migrations
Generate a migration:
rails generate migration revert_create_tablename
Write the migration:
require_relative '20121212123456_create_tablename'
class RevertCreateTablename < ActiveRecord::Migration[5.0]
def change
revert CreateTablename
end
end
This way you can also rollback and can use to revert any migration
Alternative to raising exception or attempting to recreate a now empty table - while still enabling migration rollback, redo etc -
def change
drop_table(:users, force: true) if ActiveRecord::Base.connection.tables.include?('users')
end
You can't simply run drop_table :table_name, instead you can create an empty migration by running:
rails g migration DropInstalls
You can then add this into that empty migration:
class DropInstalls < ActiveRecord::Migration
def change
drop_table :installs
end
end
Then run rails db:migrate in the command line which should remove the Installs table
The solution was found here
Open you rails console
ActiveRecord::Base.connection.execute("drop table table_name")
ActiveRecord::Base.connection.drop_table :table_name
if anybody is looking for how to do it in SQL.
type rails dbconsole from terminal
enter password
In console do
USE db_name;
DROP TABLE table_name;
exit
Please dont forget to remove the migration file and table structure from schema
I needed to delete our migration scripts along with the tables themselves ...
class Util::Table < ActiveRecord::Migration
def self.clobber(table_name)
# drop the table
if ActiveRecord::Base.connection.table_exists? table_name
puts "\n== " + table_name.upcase.cyan + " ! "
<< Time.now.strftime("%H:%M:%S").yellow
drop_table table_name
end
# locate any existing migrations for a table and delete them
base_folder = File.join(Rails.root.to_s, 'db', 'migrate')
Dir[File.join(base_folder, '**', '*.rb')].each do |file|
if file =~ /create_#{table_name}.rb/
puts "== deleting migration: " + file.cyan + " ! "
<< Time.now.strftime("%H:%M:%S").yellow
FileUtils.rm_rf(file)
break
end
end
end
def self.clobber_all
# delete every table in the db, along with every corresponding migration
ActiveRecord::Base.connection.tables.each {|t| clobber t}
end
end
from terminal window run:
$ rails runner "Util::Table.clobber 'your_table_name'"
or
$ rails runner "Util::Table.clobber_all"
Helpful documentation
In migration you can drop table by:
drop_table(table_name, **options)
options:
:force
Set to :cascade to drop dependent objects as well. Defaults to false
:if_exists
Set to true to only drop the table if it exists. Defaults to false
Example:
Create migration for drop table, for example we are want to drop User table
rails g migration DropUsers
Running via Spring preloader in process 13189
invoke active_record
create db/migrate/20211110174028_drop_users.rb
Edit migration file, in our case it is db/migrate/20211110174028_drop_users.rb
class DropUsers < ActiveRecord::Migration[6.1]
def change
drop_table :users, if_exist: true
end
end
Run migration for dropping User table
rails db:migrate
== 20211110174028 DropUsers: migrating ===============================
-- drop_table(:users, {:if_exist=>true})
-> 0.4607s
the best way you can do is
rails g migration Drop_table_Users
then do the following
rake db:migrate
Run
rake db:migrate:down VERSION=<version>
Where <version> is the version number of your migration file you want to revert.
Example:-
rake db:migrate:down VERSION=3846656238
Drop Table/Migration
run:-
$ rails generate migration DropTablename
exp:- $ rails generate migration DropProducts
if you want to drop a specific table you can do
$ rails db:migrate:up VERSION=[Here you can insert timestamp of table]
otherwise if you want to drop all your database you can do
$rails db:drop
Run this command:-
rails g migration drop_table_name
then:
rake db:migrate
or if you are using MySql database then:
login with database
show databases;
show tables;
drop table_name;
If you want to delete the table from the schema perform below operation --
rails db:rollback

How to drop columns using Rails migration

What's the syntax for dropping a database table column through a Rails migration?
remove_column :table_name, :column_name
For instance:
remove_column :users, :hobby
would remove the hobby Column from the users table.
For older versions of Rails
ruby script/generate migration RemoveFieldNameFromTableName field_name:datatype
For Rails 3 and up
rails generate migration RemoveFieldNameFromTableName field_name:datatype
Rails 4 has been updated, so the change method can be used in the migration to drop a column and the migration will successfully rollback. Please read the following warning for Rails 3 applications:
Rails 3 Warning
Please note that when you use this command:
rails generate migration RemoveFieldNameFromTableName field_name:datatype
The generated migration will look something like this:
def up
remove_column :table_name, :field_name
end
def down
add_column :table_name, :field_name, :datatype
end
Make sure to not use the change method when removing columns from a database table (example of what you don't want in the migration file in Rails 3 apps):
def change
remove_column :table_name, :field_name
end
The change method in Rails 3 is not smart when it comes to remove_column, so you will not be able to rollback this migration.
In a rails4 app it is possible to use the change method also for removing columns. The third param is the data_type and in the optional forth you can give options. It is a bit hidden in the section 'Available transformations' on the documentation .
class RemoveFieldFromTableName < ActiveRecord::Migration
def change
remove_column :table_name, :field_name, :data_type, {}
end
end
There are two good ways to do this:
remove_column
You can simply use remove_column, like so:
remove_column :users, :first_name
This is fine if you only need to make a single change to your schema.
change_table block
You can also do this using a change_table block, like so:
change_table :users do |t|
t.remove :first_name
end
I prefer this as I find it more legible, and you can make several changes at once.
Here's the full list of supported change_table methods:
http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/change_table
Clear & Simple Instructions for Rails 5 & 6
WARNING: You will lose data.
Warning: the below instructions are for trivial migrations. For complex migrations with e.g. millions of rows, read/write dbs, clusters, this advice is not for you:
1. Create a migration
Run the following command in your terminal:
rails generate migration remove_fieldname_from_tablename fieldname:fieldtype (Table name in plural, as per convention. See the documentation here. )
Example: rails g migration RemoveAcceptedFromQuotes accepted:boolean
2. Check the migration
# db/migrate/20190122035000_remove_accepted_from_quotes.rb
class RemoveAcceptedFromQuotes < ActiveRecord::Migration[5.2]
# with rails 5.2 you don't need to add a separate "up" and "down" method.
def change
remove_column :quotes, :accepted, :boolean
end
end
3. Run the migration
rake db:migrate or rails db:migrate (they're both the same)
....And then you're off to the races!
Generate a migration to remove a column such that if it is migrated (rake db:migrate), it should drop the column. And it should add column back if this migration is rollbacked (rake db:rollback).
The syntax:
remove_column :table_name, :column_name, :type
Removes column, also adds column back if migration is rollbacked.
Example:
remove_column :users, :last_name, :string
Note: If you skip the data_type, the migration will remove the column successfully but if you rollback the migration it will throw an error.
in rails 5 you can use this command in the terminal:
rails generate migration remove_COLUMNNAME_from_TABLENAME COLUMNNAME:DATATYPE
for example to remove the column access_level(string) from table users:
rails generate migration remove_access_level_from_users access_level:string
and then run:
rake db:migrate
Remove Columns For RAILS 5 App
rails g migration Remove<Anything>From<TableName> [columnName:type]
Command above generate a migration file inside db/migrate directory. Snippet blow is one of remove column from table example generated by Rails generator,
class RemoveAgeFromUsers < ActiveRecord::Migration
def up
remove_column :users, :age
end
def down
add_column :users, :age, :integer
end
end
I also made a quick reference guide for Rails which can be found at here.
You can try the following:
remove_column :table_name, :column_name
(Official documentation)
rails g migration RemoveXColumnFromY column_name:data_type
X = column name
Y = table name
EDIT
Changed RemoveXColumnToY to RemoveXColumnFromY as per comments - provides more clarity for what the migration is actually doing.
To remove the column from table you have to run following migration:
rails g migration remove_column_name_from_table_name column_name:data_type
Then run command:
rake db:migrate
remove_column in change method will help you to delete the column from the table.
class RemoveColumn < ActiveRecord::Migration
def change
remove_column :table_name, :column_name, :data_type
end
end
Go on this link for complete reference : http://guides.rubyonrails.org/active_record_migrations.html
For removing column from table in just easy 3 steps as follows:
write this command
rails g migration remove_column_from_table_name
after running this command in terminal one file created by this name and time stamp (remove_column from_table_name).
Then go to this file.
inside file you have to write
remove_column :table_name, :column_name
Finally go to the console and then do
rake db:migrate
Give below command it will add in migration file on its own
rails g migration RemoveColumnFromModel
After running above command you can check migration file remove_column code must be added there on its own
Then migrate the db
rake db:migrate
Heres one more from rails console
ActiveRecord::Migration.remove_column(:table_name, :column_name)
Step 1: Create a migration
rails g migration remove_column_name_from_table
Step 2: Change code in file migration just created
rails version < 3
def change
remove_column :table_name, :column_name, :datatype
end
rails version >= 3
def change
remove_column :table_name, :column_name
end
Step 3: Migrate
rake db:migrate
Simply, You can remove column
remove_column :table_name, :column_name
For Example,
remove_column :posts, :comment
first try to create a migration file running the command:
rails g migration RemoveAgeFromUsers age:string
and then on the root directory of the project run the migration running the command:
rails db:migrate
Through
remove_column :table_name, :column_name
in a migration file
You can remove a column directly in a rails console by typing:
ActiveRecord::Base.remove_column :table_name, :column_name
Do like this;
rails g migration RemoveColumnNameFromTables column_name:type
I.e. rails g migration RemoveTitleFromPosts title:string
Anyway, Would be better to consider about downtime as well since the ActiveRecord caches database columns at runtime so if you drop a column, it might cause exceptions until your app reboots.
Ref: Strong migration
Mark the column as ignored in the model
class MyModel < ApplicationRecord
self.ignored_columns = ["my_field"]
end
Generate a migration
$ bin/rails g migration DropMyFieldFromMyModel
Edit the migration
class DropMyFieldFromMyModel < ActiveRecord::Migration[6.1]
def change
safety_assured { remove_column :my_table, :my_field }
end
end
Run the migration
$ bin/rails db:migrate
you can use rails migration command
rails generate migration RemoveColumnNameFromTableName column_name:column_type
than you can migrate the database:
rails db:migrate
Just run this in the rails console
ActiveRecord::Base.connection.remove_column("table_name", :column_name, :its_data_type)
or
TableName.find_by_sql(“ALTER TABLE table_name DROP column_name”)

Resources