If I have command and model like this:
Command
$ bin/rails generate model User name:string firstname:string
$ bin/rails genarate model Profile username:string password:string
model generated by rails
class User < ApplicationRecord
has_one :profile
end
Class Profile < ApplicationRecord
belongs_to :user
end
Is the 1:1 association in migrate folder generated automatically by rails or I have to implement it myself?
The migration file for Profiles (class CreateProfiles < ActiveRecord::Migration) should have a foreign key declaration like this:
t.references :user, null: false, foreign_key: true
whole code:
create_table :profiles do |t|
t.string :username
t.string :password
t.references :user, null: false, foreign_key: true
t.timestamps
end
That can also be generated automaticaly with the generator command by adding user:references like so:
$ bin/rails genarate model Profile username:string password:string user:references
You can also refer to the Active Record Migrations guide and search for "references" on that page.
ActiveRecord::Associations::ClassMethods does not create database columns automatically. That would actually be a very risky and undesirable feature. You do not want your Rails server to change the DB schema at runtime. Thats what migrations are for.
You can however use the references "type" in the generators (also aliased as belongs_to) which creates a a foreign key column as well as an assocation in the model:
bin/rails g model Profile username:string password:string user:references
class CreateProfiles < ActiveRecord::Migration[7.0]
def change
create_table :profiles do |t|
t.string :username
t.string :password
t.references :user, null: false, foreign_key: true
t.timestamps
end
end
end
class Profile < ApplicationRecord
belongs_to :user
end
There is no equivilent for adding has_one or has_many assocations from generators as they don't actually correspond to a database column on this model.
If you need to add a foreign key column to the table after the fact you do it by generating a migration:
bin/rails g migration AddUserIdToProfiles user:refereces
Related
I am trying to add seller_id to my items model in a migration by doing the following:
rails g model Item title:string description:text price:bigint status:integer published_date:datetime seller:belongs_to
This didn't work of course...
ultimately I want to see seller_id in my items table, which relates the item model to the user model.
Presently my migration looks like this:
class CreateItems < ActiveRecord::Migration
def change
create_table :items do |t|
t.string :title
t.text :description
t.bigint :price
t.integer :status
t.datetime :published_date
t.belongs_to :user, index: true, foreign_key: true
t.timestamps null: false
end
end
end
How can I modify this migration to create a foreign key "seller_id" that relates to the user model.
How can I auto generate a migration that includes edits from 1., from the console? I.E modify the rails g model ... to generate the migration I need.
Huge Thanks!
Just explicitly define what you want in the migration file and add the necessary relation to your model, instead of t.belongs_to you could just use:
t.integer :seller_id, index: true, foreign_key: true
and in your models you could go about this a few ways, if you also want reference your relation as seller on item instances then in the Item class make the relation:
belongs_to :seller, class_name: 'User'
and in the User class:
has_many :items, foreign_key: :seller_id
or if you want to reference it as user from the items then:
belongs_to :user, foreign_key: :seller_id
In terms of editing the generator, that is the default way that a model generator works and in my opinion I like to keep the defaults the way they are. However I do recommend creating your own rails generators or your own rake tasks for when you want to create some custom / special case stuff. For looking at creating your own generator I would point you to the ruby on rails official guide for creating generators to get you started: http://guides.rubyonrails.org/generators.html
Inside your migration you can use
t.belongs_to :seller, foreign_key: {to_table: :users}
If you don't want it to be specifically called seller_id then what you have is enough. Once you run your migration using rake db:migrate you will see the actual table structure in your db/schema.rb file with a foreign key user_id
You can simply edit the generated migration file in your favorite text editor to edit the migration before running rake db:migrate
I wan to make simple has_many and belongs_to relation between my models.
I have User model and Quote model. And each quote belongs to user and evry user has many quotes.
I've studied this: http://guides.rubyonrails.org/association_basics.html
But I still don't know what about foreign key. Does Rails do it automatically in background (when I add has_many and belongs_to) or should I add manually migration with user_id column added to Quotes table?
Should I have user_id column in my db_schema?
The foreign keys should be defined in your migration. If you use the generator, Rails will generate a migration, which maybe looks like the following:
class CreateQuotes < ActiveRecord::Migration
def change
create_table :quotes do |t|
t.string :title, null: false
t.text :content, null: false
t.references :user
t.timestamps
end
end
end
The statement t.references :user will generate your foreign key column, which is called user_id in this case.
Here is a quote from the Rails Guides:
Using t.integer :supplier_id makes the foreign key naming obvious and explicit. In current versions of Rails, you can abstract away this implementation detail by using t.references :supplier instead.
A user has many uploads. I want to add a column to the uploads table that references the user. What should the migration look like?
Here is what I have. I'm not sure if I should use (1) :user_id, :int or (2) :user, :references. I'm not even sure if (2) works. Just trying to do this the "rails" way.
class AddUserToUploads < ActiveRecord::Migration
def change
add_column :uploads, :user_id, :integer
end
end
Relevant question except for Rails 3. Rails 3 migrations: Adding reference column?
Rails 4.x
When you already have users and uploads tables and wish to add a new relationship between them.
All you need to do is: just generate a migration using the following command:
rails g migration AddUserToUploads user:references
Which will create a migration file as:
class AddUserToUploads < ActiveRecord::Migration
def change
add_reference :uploads, :user, index: true
end
end
Then, run the migration using rake db:migrate.
This migration will take care of adding a new column named user_id to uploads table (referencing id column in users table), PLUS it will also add an index on the new column.
UPDATE [For Rails 4.2]
Rails can’t be trusted to maintain referential integrity; relational databases come to our rescue here. What that means is that we can add foreign key constraints at the database level itself and ensure that database would reject any operation that violates this set referential integrity. As #infoget commented, Rails 4.2 ships with native support for foreign keys(referential integrity). It's not required but you might want to add foreign key(as it's very useful) to the reference that we created above.
To add foreign key to an existing reference, create a new migration to add a foreign key:
class AddForeignKeyToUploads < ActiveRecord::Migration
def change
add_foreign_key :uploads, :users
end
end
To create a completely brand new reference with a foreign key(in Rails 4.2), generate a migration using the following command:
rails g migration AddUserToUploads user:references
which will create a migration file as:
class AddUserToUploads < ActiveRecord::Migration
def change
add_reference :uploads, :user, index: true
add_foreign_key :uploads, :users
end
end
This will add a new foreign key to the user_id column of the uploads table. The key references the id column in users table.
NOTE: This is in addition to adding a reference so you still need to create a reference first then foreign key (you can choose to create a foreign key in the same migration or a separate migration file). Active Record only supports single column foreign keys and currently only mysql, mysql2 and PostgreSQL adapters are supported. Don't try this with other adapters like sqlite3, etc. Refer to Rails Guides: Foreign Keys for your reference.
Rails 5
You can still use this command to create the migration:
rails g migration AddUserToUploads user:references
The migration looks a bit different to before, but still works:
class AddUserToUploads < ActiveRecord::Migration[5.0]
def change
add_reference :uploads, :user, foreign_key: true
end
end
Note that it's :user, not :user_id
if you like another alternate approach with up and down method try this:
def up
change_table :uploads do |t|
t.references :user, index: true
end
end
def down
change_table :uploads do |t|
t.remove_references :user, index: true
end
end
Create a migration file
rails generate migration add_references_to_uploads user:references
Default foreign key name
This would create a user_id column in uploads table as a foreign key
class AddReferencesToUploads < ActiveRecord::Migration[5.2]
def change
add_reference :uploads, :user, foreign_key: true
end
end
user model:
class User < ApplicationRecord
has_many :uploads
end
upload model:
class Upload < ApplicationRecord
belongs_to :user
end
Customize foreign key name:
add_reference :uploads, :author, references: :user, foreign_key: true
This would create an author_id column in the uploads tables as the foreign key.
user model:
class User < ApplicationRecord
has_many :uploads, foreign_key: 'author_id'
end
upload model:
class Upload < ApplicationRecord
belongs_to :user
end
Just to document if someone has the same problem...
In my situation I've been using :uuid fields, and the above answers does not work to my case, because rails 5 are creating a column using :bigint instead :uuid:
add_reference :uploads, :user, index: true, type: :uuid
Reference: Active Record Postgresql UUID
[Using Rails 5]
Generate migration:
rails generate migration add_user_reference_to_uploads user:references
This will create the migration file:
class AddUserReferenceToUploads < ActiveRecord::Migration[5.1]
def change
add_reference :uploads, :user, foreign_key: true
end
end
Now if you observe the schema file, you will see that the uploads table contains a new field. Something like: t.bigint "user_id" or t.integer "user_id".
Migrate database:
rails db:migrate
Another syntax of doing the same thing is:
rails g migration AddUserToUpload user:belongs_to
class MigrationName < ActiveRecord::Migration[7.0]
disable_ddl_transaction!
def change
add_reference :uploads, :user, index: {algorithm: :concurrently}
end
end
I would like to add a has_many relationship to two existing tables/models in my app & I'm not too sure how to di it?
When I did this before with a new model the rails generate command handled everything for me, with just rails generate model Photo image:string hikingtrail:references it created the below migration
class CreatePhotos < ActiveRecord::Migration
def change
create_table :photos do |t|
t.string :image
t.references :hikingtrail
t.timestamps
end
add_index :photos, :hikingtrail_id
end
end
Now I would like set up a relationship between users & photos with each user has_many :photos.
When I generate a migration to achieve this it does not include the add_index :photos, :user_id, is this something I should be doing manually or are the below steps enough for setting up this relationship in my database?
rails g migration AddUserIdToPhotos user_id:integer
which creates...
class AddUserIdToPhotos < ActiveRecord::Migration
def change
add_column :photos, :user_id, :integer
end
end
& then run...
rake db:migrate
It is enough to set up your relationship. You can add a index to improve the speed of your record searching. In fact some recommend to put a index to all the foreign keys. But don't worry about this now, i guess you are not going to have that many records to use a index.
If you have already migrated everything and want to add a index make do:
rails g migration AddIndexToUserIdToPhotos
and inside add the index column:
class AddUserIdToPhotos < ActiveRecord::Migration
def change
add_index :photos, :user_id
end
end
If I create a new rails 3 migration with (for example)
rails g migration tester title:tester user:references
, everything works fine...however if I add a column with something along the lines of:
rails g migration add_user_to_tester user:references
the reference field is not recognised. In short, the question is: how do I add a referencing column to a rails migration from the command line?
If you are using the Rails 4.x you can now generate migrations with references, like this:
rails generate migration AddUserRefToProducts user:references
like you can see on rails guides
EDIT: This is an outdated answer and should not be applied for Rails 4.x+
You don't need to add references when you can use an integer id to your referenced class.
I'd say the advantage of using references instead of a plain integer is that the model will be predefined with belongs_to and since the model is already created and will not be affected when you migrate something existing, the purpose is kind of lost.
So I would do like this instead:
rails g migration add_user_id_to_tester user_id:integer
And then manually add belongs_to :user in the Tester model
Please note that you will most likely need an index on that column too.
class AddUserReferenceToTester < ActiveRecord::Migration
def change
add_column :testers, :user_id, :integer
add_index :testers, :user_id
end
end
With the two previous steps stated above, you're still missing the foreign key constraint. This should work:
class AddUserReferenceToTester < ActiveRecord::Migration
def change
add_column :testers, :user_id, :integer, references: :users
end
end
You can use references in a change migration. This is valid Rails 3.2.13 code:
class AddUserToTester < ActiveRecord::Migration
def change
change_table :testers do |t|
t.references :user, index: true
end
end
def down
change_table :testers do |t|
t.remove :user_id
end
end
end
c.f.: http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/change_table
Running rails g migration AddUserRefToSponsors user:references will generate the following migration:
def change
add_reference :sponsors, :user, index: true
end
When adding a column you need to make that column an integer and if possible stick with rails conventions. So for your case I am assuming you already have a Tester and User models, and testers and users tables.
To add the foreign key you need to create an integer column with the name user_id (convention):
add_column :tester, :user_id, :integer
Then add a belongs_to to the tester model:
class Tester < ActiveRecord::Base
belongs_to :user
end
And you might also want to add an index for the foreign key (this is something the references already does for you):
add_index :tester, :user_id
That will do the trick:
rails g migration add_user_to_tester user_id:integer:index
You can add references to your model through command line in the following manner:
rails g migration add_column_to_tester user_id:integer
This will generate a migration file like :
class AddColumnToTesters < ActiveRecord::Migration
def change
add_column :testers, :user_id, :integer
end
end
This works fine every time i use it..
For Rails 4
The generator accepts column type as references (also available as belongs_to).
This migration will create a user_id column and appropriate index:
$ rails g migration AddUserRefToProducts user:references
generates:
class AddUserRefToProducts < ActiveRecord::Migration
def change
add_reference :products, :user, index: true
end
end
http://guides.rubyonrails.org/active_record_migrations.html#creating-a-standalone-migration
For Rails 3
Helper is called references (also available as belongs_to).
This migration will create a category_id column of the appropriate type. Note that you pass the model name, not the column name. Active Record adds the _id for you.
change_table :products do |t|
t.references :category
end
If you have polymorphic belongs_to associations then references will add both of the columns required:
change_table :products do |t|
t.references :attachment, :polymorphic => {:default => 'Photo'}
end
Will add an attachment_id column and a string attachment_type column with a default value of Photo.
http://guides.rubyonrails.org/v3.2.21/migrations.html#creating-a-standalone-migration