Conflict with foreign key on table users - php

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)

Related

Laravel migration problem : Foreign key constraint is incorrectly formed in l

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

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

Laravel how to create foreign key constrain from non-primary key

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

Error While make a relationship in mysql with 2 (varchar columns)

First table users
id name
---------------------
1 John
Second table orders
id order name
----------------------------------
1 pencil John
The sql code to make a relationship with first column name in table users
And second column name in table orders
ALTER TABLE orders
ADD CONSTRAINT user_name
FOREIGN KEY(name) REFERENCES users(name)
ON UPDATE CASCADE
ON DELETE CASCADE;
it give me error
errno: 150 "Foreign key constraint is incorrectly formed"
How to Fix this proplem ??
So after searching I found that the solution is very simple :D
use the same sql code
ALTER TABLE orders
ADD CONSTRAINT user_name
FOREIGN KEY(name) REFERENCES users(name)
ON UPDATE CASCADE
ON DELETE CASCADE;
and make name unique in the parent column .

Payum Symfony Doctrine Can't Create Table

I am trying to add a reference to a profile (called Square) to my Payment model (extension of Payum\Core\Model\ArrayObject), but I can't add a foreign key on the table. I want to do this so that when the payment is processed I can update a field saying that the square is now paid. Here is my setup:
Square.php
/**
* #var integer
*
* #ORM\Column(name="msid", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $msid;
Payment.php
/**
* #ORM\OneToOne(targetEntity="Square")
* #ORM\JoinColumn(name="msid", referencedColumnName="msid")
*/
private $square;
Error Codes:
[Doctrine\DBAL\Exception\DriverException]
An exception occurred while executing 'ALTER TABLE payment ADD CONSTRAINT FK_6D28840D405F5364 FOREIGN KEY (msid) REFERENCES square (msid)':
SQLSTATE[HY000]: General error: 1005 Can't create table 'memorysq_version2.#sql-1658_1487b' (errno: 150)
Several other entities use the msid field to join on, it just isn't working with my Payum Payment model.
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'memorysq_version2.#sql-1658_1487b' (errno: 150)
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'memorysq_version2.#sql-1658_1487b' (errno: 150)
Output of --dump-sql
ALTER TABLE comment ADD CONSTRAINT FK_9474526C405F5364 FOREIGN KEY (msid) REFERENCES square (msid);
ALTER TABLE asset ADD CONSTRAINT FK_2AF5A5C405F5364 FOREIGN KEY (msid) REFERENCES square (msid);
ALTER TABLE payment ADD CONSTRAINT FK_6D28840D405F5364 FOREIGN KEY (msid) REFERENCES square (msid);
ALTER TABLE square ADD CONSTRAINT FK_CDE368A9F132696E FOREIGN KEY (userid) REFERENCES user (userid);
ALTER TABLE square ADD CONSTRAINT FK_CDE368A96AFF851C FOREIGN KEY (squaretype) REFERENCES product (id);
ALTER TABLE square ADD CONSTRAINT FK_CDE368A9DC01AA6E FOREIGN KEY (dualpicture) REFERENCES asset (assetid);
ALTER TABLE square ADD CONSTRAINT FK_CDE368A97F98CD1C FOREIGN KEY (clientid) REFERENCES client (clientid);
ALTER TABLE square ADD CONSTRAINT FK_CDE368A916DB4F89 FOREIGN KEY (picture) REFERENCES asset (assetid);
Several other entities use the msid field to join on, it's just not working with my Payum Payment model. Any help would be greatly appreciated.
The error message actually says that MySQL can't create the foreign key. Since you're executing an ALTER TABLE, the tables should already be there. Since the tables presumably already exist, it could be that there's already some data in the payment table, which isn't in the square table.
Other stuff worth looking into is if both columns have the exact same type (including signed/unsigned) and have a matching collation. On the table level, you could look at if the engine is the same, if the charset is the same and if both tables aren't temporary tables.

Categories