I want to add to my model Treatment an column, so that it belongs_to a category:
class CreateTreatments < ActiveRecord::Migration
def change
create_table :treatments do |t|
t.string :typ
t.string :content
t.date :day
t.references :patient
t.timestamps
end
add_index :treatments, :patient_id
add_index :treatments, :category_id
end
end
i want to add:
t.references :category
and
add_index :treatments, :category_id
I tried it simply copying it in and running a migration but it didnt`t worked! I know normally i would simply make an
rails g model Treatment category:references
How can i achieve this afterwards?
You should generate another migration:
rails g migration add_category_id_to_treatments category_id:integer
You ought to edit automatically generated migration and add line on the end of change method:
add_index :treatments, :category_id
Then you should run migration.
Related
I'm using rails 5, and I've installed the gem and tried to run the migration, but I'm getting this error:
Index name 'index_rates_on_rater_id' on table 'rates' already exists
Does anyone know why this exists? This is a new site and started fresh just adding devise gem.
This is the migration file that wouldn't complete on execution rails db:migrate
class CreateRates < ActiveRecord::Migration[5.1]
def self.up
create_table :rates do |t|
t.belongs_to :rater
t.belongs_to :rateable, :polymorphic => true
t.float :stars, :null => false
t.string :dimension
t.timestamps
end
add_index :rates, :rater_id
add_index :rates, [:rateable_id, :rateable_type]
end
def self.down
drop_table :rates
end
end
The gem creates a migration that does not work in later versions of rails. In Rails 5 when you use the belongs_to and references macros they create indices and foreign keys by default.
All you really need is this:
class CreateRates < ActiveRecord::Migration[5.1]
def self.change
create_table :rates do |t|
t.belongs_to :rater
t.belongs_to :rateable, polymorphic: true
t.float :stars, null: false
t.string :dimension
t.timestamps
end
add_index :rates, [:rateable_id, :rateable_type]
end
end
You don't need up and down since Rails is smart enough to know how to rollback this migration.
I'm trying to generate 3 scaffolds:
$ rails g scaffold Artist name:string type:string bio:text resume:string
site:string
$ rails g scaffold ArtistSerie title:string artist:references
$ rails g scaffold ArtistSeriePhoto photo:string
title:string year:integer description:text dimensions:string
featured:boolean artist_serie:references
the first two models are creating their indexes and foreign keys properly, but the third one is generating this error after rake db:migrate:
Mysql2::Error: Key column 'artist_series_id' doesn't exist in table: ALTER TABLE `artist_serie_photos` ADD CONSTRAINT `fk_rails_9422e9e931`
FOREIGN KEY (`artist_series_id`)
REFERENCES `artist_series` (`id`)
here is the generated migrations:
class CreateArtists < ActiveRecord::Migration
def change
create_table :artists do |t|
t.string :name
t.string :type
t.text :bio
t.string :resume
t.string :site
t.timestamps null: false
end
end
end
class CreateArtistSeries < ActiveRecord::Migration
def change
create_table :artist_series do |t|
t.string :title
t.references :artist, index: true, foreign_key: true
t.timestamps null: false
end
end
end
class CreateArtistSeriePhotos < ActiveRecord::Migration
def change
create_table :artist_serie_photos do |t|
t.string :photo
t.string :title
t.integer :year
t.text :description
t.string :dimensions
t.boolean :featured
t.references :artist_serie, index: true, foreign_key: true
t.timestamps null: false
end
end
end
the table was created and the field artist_serie_id too but the index and foreign key don't.
I already created another blank project and it works (on sqlite) so probably it's a mysql adapter error.
Any idea?
I appreciate your help!
I expect the root problem is that series is singular. That is, serie is not the singular form of series. It might be worth replacing series with sequence.
Since I am new to Rails, I am following this 'getting started' guide on rails website.
On section 6.1 Generating a Model, I should run rails generate model Comment commenter:string body:text post:references to get a comment model.
Supposedly, this is what should be included in the migration file:
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post
t.timestamps
end
add_index :comments, :post_id
end
end
But in my migration file, I have everything but this line add_index :comments, :post_id.
Instead, I have index:true following the t.references :post
I can't seem to find an explanation to this, can anyone explain to me what is going on here? Because later on I need to use :post_id, but in my version of migration, it is not clearly declared. I am very confused.
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post
t.timestamps
end
add_index :comments, :post_id
end
end
and
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :commenter
t.text :body
t.references :post, index: true
t.timestamps
end
end
end
will do same things, both of them will add index to post_id column
also you can later add index to any column any column that you want to add to your model like
$ rails generate migration AddPartNumberToProducts part_number:string:index
that will produce this code:
class AddPartNumberToProducts < ActiveRecord::Migration
def change
add_column :products, :part_number, :string
add_index :products, :part_number
end
end
in above code if you want to create index for post_id then write it as t.integer :post_id
else you can try this add_index :comments, :post
So I am trying to create three models, Project, Entry and User. A Project has many Entries, and a a User has many Entries. I scaffolded the above three models with the following commands:
rails g scaffold Project title:string
rails g scaffold Entry project:project_id entry_for:user_id created_by:userid \
date:string start_time:string end_time:string total:string type_of_work:string \
on_off_site:string phase:string description:text
rails g scaffold User name:string
I realize that I probably totally goofed up the part where I manually put in the foreign keys for the other tables in the Entry model. I don't know how much has_many belongs_to automates the relationship between different models in terms of keys so I tried to add the foreign key fields manually. Is this wrong?
When I try to run db:migrate I get the following error:
undefined method `user_id' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x007fc1bdf37970>
Here are my migrations, as I tried to remove all of the foreign key fields but got the above error.
class CreateProjects < ActiveRecord::Migration
def change
create_table :projects do |t|
t.string :name
t.timestamps
end
end
end
class CreateEntries < ActiveRecord::Migration
def change
create_table :entries do |t|
t.string :project
t.string :project_id
t.user_id :entry_for
t.user_id :created_by
t.string :date
t.string :start_time
t.string :end_time
t.string :total
t.string :type_of_work
t.string :on_off_site
t.string :phase
t.text :description
t.timestamps
end
end
end
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.timestamps
end
end
end
class RemoveColumns < ActiveRecord::Migration
def self.up
remove_column :entries, :created_by
remove_column :entries, :entry_for
remove_column :entries, :project_id
end
def self.down
add_column :entries, :created_by, :user_id
add_column :entries, :entry_for , :user_id
add_column :entries, :project_id, :string
end
end
Thank you for your help!
In your migrations, instead of t.user_id you can try t.integer or t.index. That should at least get your migrations running.
I am going to use t.references entry_for and then write belongs_to :entry_for, class_name: "User" in the Entry model as a means of writing my own foreign key name. For the project column in entries I realized I can similarly write t.references project.
I have one table called profiles with some columns.
Now I wish to add a few columns to this table using the change-method in rails 3.1. I created a migration with the following code:
def change
change_table :profiles do |t|
t.string :photo
t.string :name
t.references :user
end
end
The migration works perfectly, but when I want to rollback I get
SQLite3::SQLException: duplicate column name: photo: ALTER TABLE "profiles" ADD "photo" varchar(255)
Any ideas why?
The auto-generated migrations for adding columns in Rails 3.1 is in the format:
class AddColumnToTable < ActiveRecord::Migration
def change
add_column :table, :column, :type
end
end
Perhaps try that syntax?
It looks like you need to tell the migration how to revert itself:
def change
change_table :profiles do |t|
t.string :photo
t.string :name
t.references :user
end
reversible do |dir|
dir.down do
remove_column :profiles, :photo
remove_column :profiles, :name
remove_column :profiles, :user_id
end
end
end
See http://guides.rubyonrails.org/migrations.html#using-reversible for more information.
Alternatively you could try using the old up and down methods which are still available like so:
def up
change_table :profiles do |t|
t.string :photo
t.string :name
t.references :user
end
end
def down
remove_column :profiles, :photo
remove_column :profiles, :name
remove_column :profiles, :user_id
end
More on up/down here: http://guides.rubyonrails.org/migrations.html#using-the-up-down-methods