I'm trying to add indexing to my users table for the email column. Typing rails g migration add_index_to_users_email generates the migration but the function is empty. The snake casing should be correct, so I'm at a loss as to why the migration is being created but the change function inside is empty.
I've also tried AddIndexToUsersName and the same issue arises.
Any direction on what the issue could be would be greatly appreciated. Only thing I can think of is that I'm using Postgres and not MySQL or SQLite, but that wouldn't matter would it?
As far as I know, migration generators only support addition and removal of columns, with a specified modifier. For example, if you wished to add a new string column phone to the users table, you could use the command
rails generate migration AddPhoneToUsers phone:string
Check the Rails Guides for column modifiers. You can try
rails generate migration AddIndexToUsers email:index
to add an index to the existing column. However, I am not sure if generators support column modification. You can write the migration yourself, assuming the email column already exists on the users table:
class AddIndexToUsers < ActiveRecord::Migration
def change
add_index :users, :email
end
end
Have a look at:
http://guides.rubyonrails.org/active_record_migrations.html
The correct command is
rails g migration AddIndexToUsers email:string:index
This will generate:
class AddIndexToUsers < ActiveRecord::Migration
def change
add_column :users, :email, :string
add_index :users, :email
end
end
Edit the migration file and delete the add_column line, then run the migration.
Related
I've already created a model in Rails to collect some user information
I created the columns as :string initially but I've since changed the way this data is looked up and entered by using separate populated models.
Now instead of entering into these fields as string - i want these columns to be "references" instead.
Is there an easy way to change from the string to reference without having to create a new model entirely?
*do not need to save the existing data
Is there any data in the strings you would like to save?
Or is it just because it has the same name?
You don't have to create a new model.
You could create a simple migration
remove_column :table, :your_column_name, :string
add_column :table, :your_column_name, :integer, references: :your_parent_model
You can add a temporary string column to save the string column first:
rails g migration add_temporary_string_column_to_model temporary_string_column:string
And run rails console:
SomeModel.all.each do |some_model|
some_mode.temporary_string_column = some_mode.string_column
some_mode.save
end
And now you can change your original string column's type to references which is an int(4) column in MySQL, migration like this:
class ChangeFormatInSomeTable < ActiveRecord::Migration
def change
change_column :some_table, :string_column, :references
end
end
Finally, you can run rails console again to convert the string data to integer like this:
SomeModel.all.each do |some_model|
some_mode.string_column = some_mode.temporary_string_column.to_i
some_mode.save
end
And at last, remove the temporary string column:
rails g migration remove_temporary_string_column_from_model temporary_string_column
Here is another solution, without dropping the column itself (not exactly in my case). I'm not sure though if this is the best solution.
In my case, I have a tickets table that holds purchase_uid in itself. I decided to keep purchases in another table after making the necessary improvements in our backend. Purchases table has uuid as the primary key. Given this background, here is my migration to change my column into a reference.
class AddPurchaseRelationToTickets < ActiveRecord::Migration[5.2]
def up
change_column :tickets, :purchase_uid, :uuid, references: :purchase, foreign_key: true, using: 'purchase_uid::uuid'
end
def down
change_column :tickets, :purchase_uid, :string
end
end
In my case, since string doesn't automatically cast into uuid, purchase_uid were dropped and recreated as well. However, if you decide to keep the column type same, I don't think it will be a problem.
You can create migrations to serve the exact purpose.
rails generate migration AddAddressToUsers address:references
This will create a migration file in db/migrate directory.
Then run: rails db:migrate to run migration and make changes in your database.
Don't forget to create associations in your models (belongs_to, has_many, etc.) depending on your system structure.
Wanted to add a simpler alternative to the accepted answer that preserves data:
class ChangeStringToInt < ActiveRecord::Migration[5.1]
def up
change_column :table_name, :field_name, :integer, null: false, references: :table_referenced, using: 'field_name::integer'
add_index :chapter_actions, :field_name
end
def down
change_column :table_name, :field_name, :string, null: false, using: 'field_name::character varying'
remove_index :table_name, :field_name
end
end
I am new to rails, and created a custom migration to change my database structure using Rails Generate. Here is the command I issued: rails g migration users.
Now, in the file it created, I inputed:
class Users < ActiveRecord::Migration
def change
add_column :first_name
add_column :last_name
remove_column :name
end
end
When I run rake db:migrate nothing happens. What do I need to do to fix this?
It's not running at all? It's hard to say based on the info you gave. Perhaps you should try a migration with a more unique name? Something like:
rails g migration ConvertUsersNamesToSingleField
I'm not sure if it's cool to have two migrations with the same name. But with short generic names like Users that might be the problem here. And it usually can't hurt to have a verbose and descriptive migration name, for posterity and clarity.
This questions agree that migration with non unique names don't work: Rails migrations with the same name
But even when ran, this will raise errors. You need to include table names in those column calls, and you need to specify a type when creating fields.
class ConvertUsersNamesToSingleField < ActiveRecord::Migration
def change
add_column :users, :first_name, :string
add_column :users, :last_name, :string
remove_column :users, :name
end
end
Hi I created a table for a wiki application that I was doing. It was pretty simple in the beginning I had a Questions table and an Answers table. I now want to add users to it. So for that I created a users table and the whole signup thing is ready. The wiki part is working and the users part is working but I am having trouble to merge them together.
I first created a Questions as a scaffold as follows:
rails g scaffold Question title:string body:string
Then Answers as follows:
rails g scaffold Answer question_id:integer content:string
I then tried to add the users_id column after creating the Users table:
rails generate migration add_users_id_to_questions user_id:integer
I then tried to index the user_id column by adding the following line in the migration file:
class AddUserIdToQuestions < ActiveRecord::Migration
def change
add_column :questions, :user_id, :integer
end
add_index :questions, :user_id
end
I do rake:db migrate but it doesnt show the change after I run migration. Is there any other way I can index user_id other than by adding a column to the migration file. I even ran rake db:rollback so the addition of user_id column is gone but when I make the change and run rake db:migrate it gives me an error user_id doesnt exist. It would be greatly appreciated if you could help me how to create the index. I running Rails 3, SQLite as my DB.
Thank you.
Rollback and then re-run the migration with add_index inside the change block.
class AddUserIdToQuestions < ActiveRecord::Migration
def change
add_column :questions, :user_id, :integer
add_index :questions, :user_id
end
end
I created a model with an attribute "name" but I want to change it to "username". Everything I've read about database migrations involves creating a class or some complicated stuff. All I want to do is the equivalent of "UPDATE TABLE" in SQL. How do you run a one-time database migration to change this? I'm guessing it'd involve rails console and then some command?
First:
rails g migration rename_name_column_to_username
Then in the generated rename_name_column_to_username.rb migration file:
class RenameNameColumnToUsername < ActiveRecord::Migration
def self.up
rename_column :users, :name, :username
end
def self.down
rename_column :users, :username, :name
end
end
And then rake db:migrate
If you haven't committed the code that originally created the "name" column, you can just go in to the old migration file that created that column and change name to username and then regenerate the schema.
But if you have committed the code, you should create a separate migration file that renames name to username.
This is important to keep track of the versioning of your database. So you should never really use manual SQL (ALTER TABLE ...) to change the schema.
Run rails g migration RenameNameToUsername, which will create a new file in db/migrate.
Open that file, and add this into the self.up section:
rename_column :tablename, :name, :username
Then run rake db:migrate
ok i am a noob and i want to know how to add fields to a migration in rails 3, additionally i am using Typus and would like to know if i need to add these fields manually or can i just regenerate the typus and it will just pick the new fields up?
Thanks in advance
Robbie
Migrations are used to add fields to the database, not tell Rails about fields.
Rails will actually inspect the table for its fields, so if you have an existing table, you can create a model called spy.rb and it will know about all fields in spies
To use migrations, run rails generate migration AddScreenshotColumns. Then that file can become:
class AddScreenshotColumnsToTemplate < ActiveRecord::Migration
def self.up
add_column :templates, :screenshot_file_name, :string
end
def self.down
remove_column :templates, :screenshot_file_name
end
end