简体   繁体   中英

Rails belongs_to and has_many is not creating a primary foreign key relation

I am creating a primary -> foreign key relationship among my columns while doing the db design. my db design should be:

Users table: userid (primary key), name

Shoppinglist table: shoppingid (primary key), userid(foreign key)

Here is my migration file:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.integer :userid, :primary_key
      has_many :shoppinglists, dependent: :destroy

      t.timestamps null: false
    end
  end
end

class CreateShoppinglists < ActiveRecord::Migration
  def change
    create_table :shoppinglists do |t|
      t.integer :shoppingid, :primary_key
      t.belongs_to :userid

      t.timestamps null: false
    end
  end
end

Isnt there a way to create the above while creating the table instead of creating a new migration file?

I use this command to check my relationships and I dont see any relations created:

User.reflect_on_all_associations

Since your migration fails to apply, you may have concluded that it contains errors. You seem to not understand the purpose of migrations and their difference with models. Plus, you're violating plenty of conventions, so it does a bit of what you're not expecting.

  • has_many and belongs_to are not migrations' scope. These methods are from models and all they add is some query methods. They can even be used on an existing database without the migration mechanism at all, as horrible as that may be.
  • Rails already adds a primary key column id to every table, unless told otherwise with an option id: false in create_table . So add another one and violate conventions at your own peril.
  • Rails' convention of column naming is that of method naming: snake_case , not likethis . It uses this to properly pluralize and singularize the given terms to infer configuration through convention. Consequently, foreign key columns are specified in form <ASSOCIATION>_id , ie user_id .
  • Foreign key support has come quite recently, in Rails 4.2 only , and since it doesn't (as guides claim) work with sqlite , Rails don't add these by default. When following conventions, you only need to specify affected tables, Rails will infer the rest. Of course, make sure you're running Rails 4.2 (or newer) before trying. Before that, the notion of primary-foreign keys belonged not to the database, but to the models that were housing their own queries generated via has_many and the company.

You can create a migration something like:

rails g migration addShoppingListAssociationToUsers

And have something similar to following as the content of the migration:

class AddShoppingListAssociationToUser < ActiveRecord::Migration
  def change
    add_reference :shoppinglists, :user, index: true, foreign_key: true
  end
end

I am assuming that you do not need to create primary keys explicitly and can use the ones that are automatically generated with each table by Rails.

# User.rb
has_many :shopping_lists

# ShoppingList.rb
belongs_to :user

# Migration Files
class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.timestamps null: false
    end
  end
end

class CreateShoppinglists < ActiveRecord::Migration
  def change
    create_table :shoppinglists do |t|
      t.references :user, index: true
      t.timestamps null: false
    end
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM