MySql error #1005 - Can't create table - php

I write a query when doing a tutorial Agile web application development with yii. I was trying to alter a table with this query and got this error in phpmyadin
#1005 - Can't create table 'trackstar_dev.#sql-152_16' (errno: 121) (Details...)
Here is my query
ALTER TABLE `tbl_project_user_assignment` ADD CONSTRAINT `FK_project_
user` FOREIGN KEY (`project_id`) REFERENCES `tbl_project` (`id`) ON
DELETE CASCADE ON UPDATE RESTRICT
Can Anyone help please ?

This happens when table tbl_project_user_assignment has already records which project_id is not found on the table (which you want to reference) tbl_project.id. The best way you can do is to empty the tbl_project_user_assignment and alter it again to add the constraint.

Related

Laravel migration relation

I have problems with laravel migrations. I want to set a relation between 2 tables. For example, I have table users and table products, and in the products table, I have user_owner column so I can specify the user like so:
Users table
$table->bigIncrements('id');
Product table
$table->integer('user_owner');
$table->foreign('user_owner')->references('id')->on('users');
But every time when I want to migrate this table I have error like:
SQLSTATE[HY000]: General error: 1005 Can't create table `ecomet_html`.`#sql-3c38_b9` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `products` add constraint `products_store_id_foreign` foreign key (`store_id`) references `stores` (`id`))
Why am I doing wrong?
give this a try.
Update your user_owner from:
$table->integer('user_owner');
To:
$table->unsignedBigInteger('user_owner');
Check if products table is alphabeticaly before user table (migration .php files). Artisan is taking them one by one and possibly it takes products before users and then database cannot detect users table because its not created yet.
Check keys formats! Lenghts of both IDs from Users and Products are different. Try (product table):$table->bigInteger('user_owner'); or $table->unsignedBigInteger('user_owner'); or $table->bigInteger('user_owner')->unsigned();
Another suggestion is to do: $table->foreign('user_owner')->references('id')->on('users')->onDelete('cascade');, db engine could require onDelete argument

MySQL Error Code 1215: Cannot add foreign key Constraint on ALTER TABLE : Mysql

I want to add foreign key Constraint on my existing tables(type InnoDB)
,I have two tables listed below:
tbl_users : [ user_id(INT,AUTO_INCREMENT), user_name,--- etc]
tbl_users_meta : [ user_meta_id(INT,AUTO_INCREMENT), user_id(INT),--- etc]
I have already created index 'user_id' on 'tbl_users_meta' listed below :
Here is my Query but i am getting (MySQL Error Code 1215: ) each time.what is the problem i can getting it ?
ALTER TABLE `tbl_users_meta`
ADD CONSTRAINT `fk users user_id`
FOREIGN KEY (`user_id`) REFERENCES
`sanskrut`.`tbl_users`(`user_id`)
ON DELETE NO ACTION ON UPDATE CASCADE;
TRY with the same query you have writen with a small modification:
ALTER IGNORE TABLE `tbl_users_meta`
ADD CONSTRAINT `fk users user_id`
FOREIGN KEY (`user_id`) REFERENCES
`sanskrut`.`tbl_users`(`user_id`)
ON DELETE NO ACTION ON UPDATE CASCADE;
Hope it'll work
I can't see anything wrong with what you've written, so there must be something you haven't posted affecting the problem. For example, on MySQL version 5.5.44, I can successfully execute the following:
CREATE DATABASE sanskrut;
USE sanskrut;
CREATE TABLE tbl_users (user_id INT AUTO_INCREMENT PRIMARY KEY, user_name VARCHAR(50));
CREATE TABLE tbl_users_meta (user_meta_id INT AUTO_INCREMENT PRIMARY KEY, user_id INT);
CREATE INDEX user_id ON tbl_users_meta (user_id);
ALTER TABLE `tbl_users_meta`
ADD CONSTRAINT `fk users user_id`
FOREIGN KEY (`user_id`) REFERENCES
`sanskrut`.`tbl_users`(`user_id`)
ON DELETE NO ACTION ON UPDATE CASCADE;
...which finishes with your exact problem statement, copied and pasted, and works just fine.
I'd suspect, as I mentioned in the comments, that there's an index missing or possibly a datatype mismatch. You may find it helpful to add the output of:
DESC tbl_users;
DESC tbl_users_meta;
SHOW indexes FROM tbl_users;
SHOW indexes FROM tbl_users_meta;
...to your question.

How to add "ON UPDATE SET NULL" to foreign key constraint?

How can I use the Laravel Schema Builder to add "ON UPDATE SET NULL" to my foreign key constraint?
I do not understand why to set as null on update; but this is the idea:
You can delete the existent constraint.
ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol;
And create it again
ALTER TABLE tbl_name
ADD CONSTRAINT my_new_FK
FOREIGN KEY (column_in_this_table)
REFERENCES other_table (other_table_pk_column)
ON DELETE SET NULL;
You can find more information about constraints and the alter table statement in the mysql documentation.
Using FOREIGN KEY Constraints
ALTER TABLE Syntax

On delete cascade is not working

I have two table: users and 'bills'. In bills as a foreign key to users.
I want to automatically delete rows from the bills table when I delete from the users table. For that I alter table with following query, still it doesn't delete entry from bills table.
My alter statement is:
ALTER TABLE bills
ADD CONSTRAINT fk_pid
FOREIGN KEY (pid)
REFERENCES users(id)
ON DELETE CASCADE
here pid is foreign key in bills table, whereas id primary key in users table
Please help me to resolve above issue, thank you in advance.
Use create instead of alter,Otherwise your syntax is ok
Create TABLE bills(
Your columns details
------
------
ADD CONSTRAINT fk_pid
FOREIGN KEY (pid)
REFERENCES users(id)
ON DELETE CASCADE
)
Try this..
If it's doesn't work then try for this thing as well.
if your both tables having mismatch primary key and foreign key problem then you cannot add Delete Cascade.For that you need to fix that key problem.like you don't have primary key value in your user table and you are using that same id in your bills table as a foreign key then you cannot add cascade in bills table.For that remove that key from bills table and then try your adding cascade script with Alter.I had same problem but i use this way and it worked.Hope it will work for you as well.Thanks
I think this solution for create can help you in figuring out what actually you are missing.
Go thru it and try customizing it as per your need, as you haven't provided enough detail
along with your question.
I suspect it has to do something with your create table statement
OR try this for ALTER1
OR ALTER2

phpmyadmin doesnt allow me to add primary/foreign key relationship

That is the primary table field (tasks table):
task_id int(10) UNSIGNED No None AUTO_INCREMENT
This is my foreign table field (url_error_stats table):
task_id int(10) UNSIGNED No None
url_error_stats doesnt present the "relation view" option to connect between the keys..why?
SQL query:
ALTER TABLE url_error_stats ADD FOREIGN KEY ( task_id )
REFERENCES aws_backlinks.tasks (
task_id ) ON DELETE CASCADE ON UPDATE CASCADE ;
MySQL said:
1452 - Cannot add or update a child row: a foreign key constraint
fails (aws_backlinks., CONSTRAINT #sql-6f0_3bd_ibfk_1 FOREIGN KEY
(task_id) REFERENCES tasks (task_id) ON DELETE CASCADE ON UPDATE
CASCADE)
you have to use innodb and index the primary key if you want to create the foreign keys. and I will recommend you to use NAVICAT . its much easier to create foreign keys and quick too. but for a quick phpmyadmin guide see
Setting up foreign keys in phpMyAdmin?
Another reason can be the unrelated data in your tables. I mean you may have a foreign key that doesn't exist in parent table.
in this , click on url_error_stats table, then on right hand side it will show all the fields list, so now check on checkbox of that particular field which u wanted to be foreign and click on the link relation view (which is provided by phpmyadmin below to the table fields with blue color hyperlink).
it will open relationship screen , there You can select the field of the primary table.
Thanks

Categories