Mysql error 1452 – Cannot add or update a child row: a foreign key constraint fails

Quite likely your sourcecodes_tags table contains sourcecode_id values that no longer exists in your sourcecodes table. You have to get rid of those first. Here’s a query that can find those IDs: SELECT DISTINCT sourcecode_id FROM sourcecodes_tags tags LEFT JOIN sourcecodes sc ON tags.sourcecode_id=sc.id WHERE sc.id IS NULL;

Migration: Cannot add foreign key constraint

Add it in two steps, and it’s good to make it unsigned too: public function up() { Schema::create(‘priorities’, function($table) { $table->increments(‘id’, true); $table->integer(‘user_id’)->unsigned(); $table->string(‘priority_name’); $table->smallInteger(‘rank’); $table->text(‘class’); $table->timestamps(‘timecreated’); }); Schema::table(‘priorities’, function($table) { $table->foreign(‘user_id’)->references(‘id’)->on(‘users’); }); }

What’s wrong with foreign keys?

Reasons to use Foreign Keys: you won’t get Orphaned Rows you can get nice “on delete cascade” behavior, automatically cleaning up tables knowing about the relationships between tables in the database helps the Optimizer plan your queries for most efficient execution, since it is able to get better estimates on join cardinality. FKs give a … Read more

INSERT statement conflicted with the FOREIGN KEY constraint – SQL Server

In your table dbo.Sup_Item_Cat, it has a foreign key reference to another table. The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table. If you have SQL Server Management Studio, open it up and sp_help ‘dbo.Sup_Item_Cat‘. See which … Read more

How to find all tables that have foreign keys that reference particular table.column and have values for those foreign keys?

Here you go: USE information_schema; SELECT * FROM KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_NAME = ‘X’ AND REFERENCED_COLUMN_NAME = ‘X_id’; If you have multiple databases with similar tables/column names you may also wish to limit your query to a particular database: SELECT * FROM KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_NAME = ‘X’ AND REFERENCED_COLUMN_NAME = ‘X_id’ AND TABLE_SCHEMA = ‘your_database_name’;

Add a reference column migration in Rails 4

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: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)