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 895333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:32:30+00:00 2026-05-15T14:32:30+00:00

I’m working on a Rails web application with a MySQL database. I’m using migrations

  • 0

I’m working on a Rails web application with a MySQL database. I’m using migrations to modify the schema. I’ve just run into a problem wherein I’m getting an invalid record error on inserting a new record. The relevant tables:

users
id | name | email | ...

academic_records
id | scholar_id | gpa | ...

academic_records.scholar_id is a foreign key reference to users.id, but I haven’t placed any other constraints on that table. The error looks like this:

Mysql::Error: Cannot add or update a child row: a foreign key constraint fails
 (`my_db`.`academic_records`, CONSTRAINT `academic_records_ibfk_1`
 FOREIGN KEY (`id`) REFERENCES `academic_records` (`id`) ON DELETE CASCADE): ...

I opened up the MySQL database using Sequel Pro and found this constraint:

CREATE TABLE `academic_records` (
  `gpa` varchar(10) DEFAULT NULL,
  ...
  PRIMARY KEY (`id`),
  KEY `index_academic_records_on_scholar_id` (`scholar_id`),
  CONSTRAINT `academic_records_ibfk_1` FOREIGN KEY (`id`)
    REFERENCES `academic_records` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=177 DEFAULT CHARSET=utf8;

I have no idea how it got there or what it does. Why would the table have a foreign key from its own ID to itself? Can I remove the constraint? If so, how? I could see why I would want to cascade deletes from users.id to academic_records.scholar_id, but as-is, it makes no sense to me.

  • 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-05-15T14:32:30+00:00Added an answer on May 15, 2026 at 2:32 pm

    Check your migration files to see if the constraint was created there, or perhaps your project is using some plugin or RubyGem that does something with database constraints. If both of those areas turn up blanks then it must have been created by hand or by an external SQL script.

    Rails doesn’t generate or use database constraints to enforce model relationships. Database referential integrity is something you have to add yourself if you want it.

    I don’t see any reason why you can’t drop this constraint using:

    mysql> ALTER TABLE academic_records DROP FOREIGN KEY `academic_records_ibfk_1`;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 432k
  • Answers 432k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I implemented a custom dialog by simply displaying a form… May 15, 2026 at 2:33 pm
  • Editorial Team
    Editorial Team added an answer they are written with VB.NET http://msdn.microsoft.com/en-us/library/ms345242.aspx May 15, 2026 at 2:33 pm
  • Editorial Team
    Editorial Team added an answer Wow, this was seriously underdocumented. The answer is to: In… May 15, 2026 at 2:33 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.