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.
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 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
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 currently working in a new version of a project that contains an existing database, so I need to create entities trying to minify the database changes. My main problem is I want to create an optional association between two Entities but we I run doctrine:scheme:update command, it throws me the next error:
[Doctrine\DBAL\DBALException]
An exception occurred while executing 'ALTER TABLE table_A ADD CONSTRAINT
FK_AAB363B1DD3B1998 FOREIGN KEY (b_id) REFERENCES table_B
(id)': SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update
a child row: a foreign key constraint fails (`database`.`#sql-7d0_1881`, CONSTRAINT
`FK_AAB363B1DD3B1998` FOREIGN KEY (`b_id`) REFERENCES
`table_B` (`id`))
[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child
row: a foreign key constraint fails (`database`.`#sql-7d0_1881`, CONSTRAINT
`FK_AAB363B1DD3B1998` FOREIGN KEY (`b_id`) REFERENCES
`table_B` (`id`))
As you can see in the error, Class A has a property that contains an object on Entity B. This association must be unidirectional.
When I declare this type of association, I'm using this code:
/**
* #var B
*
* #ORM\ManyToOne(targetEntity="B")
* #ORM\JoinColumn(name="b_id", referencedColumnName="id")
*/
I want to make this association optional because this field is not required at all.
I've tried using nullable attribute, but reading Doctrine docs, it seems its default value is true, so it should work for me, but for any reason it doesn't.
I would appreciate any advice, code snippet or anything you can tell me about this issue.
Regards and thank you in advance.
EDIT:
I already checked this column has NULL attribute:
http://i.imgur.com/gGSOqTm.png
Instead of running doctrine:scheme:update run doctrine:scheme:update --dump-sql
Then run manually the output SQL code and ignore the irrelevant ALTER TABLE table_A ADD CONSTRAINT commands
I am having an issue running doctrine:schema:update --force because of a forgeign key constraint issue.
[Doctrine\DBAL\DBALException] An exception occurred while executing 'ALTER TABLE Product ADD CONSTRAINT FK_1CF73D312ADD6D8C FOREIGN KEY (supplier_id) REFERENCES Supplier (id) ON DELETE SET NULL':
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`wic_dev`.`#sql-5c0 a_1a12`, CONSTRAINT `FK_1CF73D312ADD6D8C` FOREIGN KEY (`supplier_id`) REFERENCES `Supplier` (`id`) ON DELETE SET NULL)
[PDOException] SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`wic_dev`.`#sql-5c0 a_1a12`, CONSTRAINT `FK_1CF73D312ADD6D8C` FOREIGN KEY (`supplier_id`) REFERENCES `Supplier` (`id`) ON DELETE SET NULL)
I have two tables that are creating this error: Products and Suppliers.
Products can have 1 supplier and suppliers can have many products.
Here is how I have my entities set up:
Product Entity:
/**
* #ORM\ManyToOne(targetEntity="WIC\SupplierBundle\Entity\Supplier", inversedBy="products", fetch="EAGER")
* #ORM\JoinColumn(name="supplier_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
* #Common\Versioned
* #Assert\NotBlank(message="Supplier Cannot Be Blank")
*/
protected $supplier;
Supplier Entity
/**
* #ORM\OneToMany(targetEntity="WIC\ProductBundle\Entity\Product", mappedBy="supplier", cascade={"all"})
*/
protected $products;
I currently have data in each table. I know some products are missing a supplier and suppliers are missing products.
What am I doing wrong and how can I fix this issue? I need to run this schema update so that my other tables will get updated as well.
Thanks so much for your help!
Here is how I fixed the issue. In my products table I was storing values as "0" if the supplier wasnt found. Because it was a manytoone and onetomany relationship between the two tables, it was having a conflict because supplier_id is never 0, 0 does not match any id in the suppliers table. I had to update the products table to set any value for 0 to NULL, this made the schema update work.
In most cases, it's a simple autoloading problem
In most cases, simply make sure you have a use statement for the entity you're trying to create a relationship with.
This happens often when the 2 entities are not in the same bundle and therefore need to autoload them. For the error to disappear, simply do this:
In your product entity, don't forget:
use WIC\SupplierBundle\Entity\Supplier;