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;
Related
I have 3 tables; orders, products and order_product. I am trying to seed the order_product pivot table using the Seeder class.
This is what I have in my code;
$productArray = \Illuminate\Support\Facades\DB::table('products')->pluck('id')->toArray();
factory(App\Order::class, 60)->create()->each(function ($order) use ($productArray) {
$randomPickings = mt_rand(1, 4);
$order->products()->sync(array_rand($productArray, $randomPickings));
});
I want attach product ids from the $productArray so an order can have, say, 1,2,3,4 or 5 products attached after seeding. Currently, I get this error when I run the seeder.
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails
(`foo`.`order_product`, CONSTRAINT
`order_product_product_id_foreign` FOREIGN KEY (`product_id`)
REFERENCES `products` (`id`) ON DELETE CASCADE) (SQL: insert into
`order_product` (`order_id`, `product_id`) values (2, 0))
There's no product with id 0. I'm guessing it's picking the indexes instead of the values. How do I get the values selected/picked instead? Thanks.
I have a foreign relationship with category_id column in database but while deleting i get error.
This is my code for delete:
public function destroy($id)
{
$category = Category::find($id);
$category->delete();
Session::flash('success', 'The category was successfully deleted.');
return redirect()->route('categories.index');
}
Error i seen is :
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`fitilicious`.`products`, CONSTRAINT `products_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`)) (SQL: delete from `categories` where `id` = 2)
Please help.
My bet is on the foreign key being set to ON DELETE RESTRICT instead of CASCADE.
Cannot delete or update a parent row: a foreign key constraint fails (fitilicious.products, CONSTRAINT products_category_id_foreign FOREIGN KEY (category_id) REFERENCES categories (id)) (SQL: delete from categories where id = 2)
This tells us that there is a row in table "products" which is referencing the category you are attempting to delete.
ON DELETE CASCADE will also delete the product.
ON DELETE RESTRICT will prevent deletion of a non-empty category
ON DELETE SET NULL will delete the category and set category_id to NULL in products table
Each has its uses, but you need to choose which one you need.
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.
I have a table in MySQL which have a ManyToMany field and I want truncate the table, but when I try it, I obtain the following error:
ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint ...
I'm using Symfony with Doctrine but if it is possible, I'm interested in learn how to do it through console
class Project {
/**
* #ORM\ManyToMany(targetEntity="Shipping", mappedBy="projects")
**/
private $employee;
}
class Employee{
/**
* #ORM\ManyToMany(targetEntity="Product", inversedBy="employee")
* #ORM\JoinTable(name="middle_table")
**/
protected $projects;
}
Foreign key means that you have two table and each update must be compatible with the table referred to by the foreign key constraint.
Posible solution is here: How do I truncate tables properly?
SET FOREIGN_KEY_CHECKS = 0; -- Disable foreign key checking.
TRUNCATE TABLE forums;
TRUNCATE TABLE dates;
TRUNCATE TABLE remarks;
SET FOREIGN_KEY_CHECKS = 1; -- Enable foreign key checking.
Now your foreign key in middle_table does not allow you to delete records from Projects table. In other words you have a link to Project in middle_table, it does not give a chance to delete row from Project. So you should change definition of your foreign key to allow deletion, you can set to null the link or make cascade delete. I prefer second option, so change annotation to following, it should allow you to delete rows in Projects table, it also will delete link to Project in middle_table.
class Project {
/**
* #ORM\ManyToMany(targetEntity="Shipping", mappedBy="projects", cascade={"remove"})
**/
private $employee;
}
Documentation:
Doctrine association mapping
mysql foreign key contraint
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