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
Related
I make the same data type for both of that table but there is a problem. But for another table I didn't get any error. what is the problem behind?
General error: 1005 Can't create table meal_system.amounts (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table amounts add constraint amounts_member_id_foreign foreign key (member_id) references members (member_id) on delete cascade)
You need first to migrate members before amounts table case your foreign is with member_id to migrate members first just change the date of the migration file to be before the amount
For example :
2021_01_01_000000_create_members_table
2021_02_02_100000_create_amounts_table
I have a table named header
$table->increments('id');
$table->integer('restaurant_number')->unsigned();
$table->integer('transaction_id')->unsigned()->unique(); <-- Right here
On docs it says
Laravel also provides support for creating foreign key constraints,
which are used to force referential integrity at the database level.
For example, let's define a user_id column on the posts table that
references the id column on a users table:
But on my details table I referencing the transaction_id from header which is not primary key and I got error this error (errno: 150 "Foreign key constraint is incorrectly formed"). How can reference it from a non-primary
$table->increments('id');
$table->integer('restaurant_number')->unsigned();
$table->integer('transaction_id')->unsigned()->nullable();
$table->foreign('transaction_id')->references('transaction_id')->on('header');
Maybe the problem is because both table as to be of the exact same types. I can see that in the second table the field is nullable and in the first is not. Maybe this is the problem
I have the users table, generated by laravel and a store table, I want all users to have a foreign key from the id store, but when I add the id it tells me that there has been an error with the foreign format ... With other tables if it works, it is only with the users that gives me conflict, I do not know if it could be because it is created before the table stores, but still I have changed the name to do the store migration before, but nothing , still the same. Thanks for the help!
> [Illuminate\Database\QueryException] SQLSTATE[HY000]: General
> error: 1005 Can't create table `Shop`.`#sql-17ec_5` (errno: 150
> "Foreign key constraint is incorrectly formed") (SQL: alter table
> `users` add constraint `users_store_id_foreign` foreign key (`store_id
> `) references `store` (`id`))
>
>
>
> [PDOException] SQLSTATE[HY000]: General error: 1005 Can't create
> table `shop`.`#sql-17ec_5` (errno: 150 "Foreign key constraint is
> incorrectly formed")
In case of small data:
Check if the foreign id you put in users table is already exists as id in store table or not.
In case of bulk data:
First remove the foreign key check.
Second Update all the users table with their Store Ids.
Third Run a Manual check by a select query to export a list of non existed Store ids of useres in the main table of stores.
Select user.ID, user.name, user.Store_ID from users
Where Store_ID not IN (Select ID from Store)
I'm designing a website of book library for my project in php.
Anyways I have three tables named books,users and savebook.
Table book has following columns: "bookid", "title","author" "genre" and "summary".
And table users has following columns userid, username, password, and name.
Users can save books as favorites and those saved books are saved in the table named savebook with columns bookid and userid which are foreign key to table book and users
I used following query for that:
ALTER TABLE savebook
ADD CONSTRAINT bkid_usid
FOREIGN KEY (bookid)
REFERENCES books (bookid);
and
ALTER TABLE savebook
ADD CONSTRAINT usid_bkid
FOREIGN KEY (userid)
REFERENCES users(userid);
Now the problem is whenever i try to delete a book from table book using query
DELETE FROM books
WHERE bookid=1;
I get this message:
1451 - Cannot delete or update a parent row: a foreign key constraint fails (booklibrary.savebook, CONSTRAINT bkid_usid FOREIGN KEY (bookid) REFERENCES books (bookid))
How do i delete a book from table book which also deletes the related row in table savebook?
To get the behavior you describe, you can specify
ON DELETE CASCADE
as part of the foreign key definition.
Reference: http://dev.mysql.com/doc/refman/5.5/en/create-table-foreign-keys.html
If you were to modify your foreign key constraint bkid_usid (on the booksave table), then the delete statement that you show
DELETE FROM books WHERE ...
Would cause MySQL to delete the rows in booksave that have a foreign key values that reference rows being removed from books.
ALTER TABLE savebook
ADD CONSTRAINT bkid_usid
FOREIGN KEY (bookid)
REFERENCES books (bookid)
ON DELETE CASCADE
;
Make note of that last line I added to the ALTER from the original question, it is an optional part of the foreign key definition (there is also ON UPDATE ...). By default, when not specified, I believe MySQL treats it as NO ACTION or RESTRICT (those two are effectively the same as far as I know) instead of CASCADE. Full documentation found at http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
An interesting question :)
I think the problem is caused by the so called "referential integrity".
That means, that you are not able to delete a dataset, which primary key is used in another tables dataset as foreign key. If you could do this, some foreign keys of the table savebook would refer to a dataset in table books, that doesn't exist anymore.
So first you have to delete all datasets in savebook, where the bookid is the same one as of the book you want to delete and after that, you can delete this dataset from books. ;)
You have applied constraints to the tables. You need to tell MySQL to delete referenced entries from "savebook" table if any entry is deleted from "book" table.
ALTER TABLE savebook ADD CONSTRAINT bkid_usid FOREIGN KEY (bookid) REFERENCES books (bookid) ON DELETE CASCADE
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.