Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8533593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:59:51+00:00 2026-06-11T09:59:51+00:00

How do I create foreign keys in ActiveRecord? I have something like the following

  • 0

How do I create foreign keys in ActiveRecord? I have something like the following in my models:

class Student < ActiveRecord::Base
  attr_accessible :name, :level_id
  belongs_to :level
end

class Level < ActiveRecord::Base
  attr_accessible :number
  has_many :students
end

But the schema.rb and development sqlite3 database don’t have any indication foreign key constraints were setup with the level_id field. Is this something I have to do manually apart from ActiveRecord or Rails? Did I miss a step?

Using Rails 3.2.8

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:59:53+00:00Added an answer on June 11, 2026 at 9:59 am

    You do not need a foreign key constraints for ActiveRecord to correctly map the relationships. You can use validations to have the Rails app ensure data integrity.

    Rails migration do not provider helpers to create foreign keys. You can create your own SQL for the constraint in the migration or use the the Foreigner Gem. Foreigner will provide helper methods for creating constraints in a migration:

    add_foreign_key(:students, :levels)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running activerecord 3.0.0beta. I know you can create columns with foreign keys like
I have an h2 database with two tables with foreign keys like CREATE TABLE
hi i have a database with many tables and foreign keys like this CREATE
when i create a model with a foreign key relationship, something like this: class
I have three models linked by foreign keys: class One(models.Model): ... class Two(models.Model): one
I'd like to do something like this: class Task(models.Model): ... created_by = models.ForeignKey( User,
I have an event model which has city, state, country as foreign keys. Like
I have a table containing 2 entries. Something like CREATE TABLE `db`.`main` ( `id`
We have several tables with foreign keys. I've checked their CREATE statements, and there
My end goal is to accomplish something like: CREATE FOREIGN KEY IF NOT EXISTS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.