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
Related
I have been stuck on this error
Failed to save dataSQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign
key constraint fails (ticketdatabase.ticket, CONSTRAINT ticket_ibfk_4 FOREIGN KEY (EventId) REFERENCES event
(id) ON DELETE CASCADE ON UPDATE CASCADE)"
my sql code is
INSERT INTO ticket (TicketId,EventId,StatusId,Price,UserId,TypeId) VALUES (?,?,?,?,?,?);
the ticket table has a relationship with event table. and the value that i insert actually is already exists on event table but still shows this error.
when i manually insert in php myadmin it inserts fine. but trough the code it raise the error. any help please
I am trying to add FK 'module_id' to my table 'documents'. I have ran the following query:
public function up()
{
Schema::table('documents', function (Blueprint $table) {
$table->integer('module_id')->unsigned();
$table->foreign('module_id')->references('id')->on('modules');
});
}
The following error is being returned:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
(SQL: alter table documents add constraint
documents_module_id_foreign foreign key (module_id) references
modules (id))
I'm not sure what I'm doing wrong, I'm sure it is probably a silly mistake but I have spent a lot of time going around in circles trying to figure it out... here is what I have tried..
both tables are already created
the data types for both column's are consistent (both unsignedBigInts, 20)
I have included a picture of my DB tables, i appreciate any help.
Update:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails
(laravel.#sql-2cd_23, CONSTRAINT documents_module_id_foreign
FOREIGN KEY (module_id) REFERENCES modules (id)) (SQL: alter
table documents add constraint documents_module_id_foreign foreign
key (module_id) references modules (id))
The type of the column needs to be a big integer.
Schema::table('documents', function (Blueprint $table) {
$table->unsignedBigInteger('module_id');
$table->foreign('module_id')->references('id')->on('modules');
});
Update
You probably already got data in your tables, since the column can't be null the foreign key can't exists. Starting it out as nullable then adding the relationships and removing the nullable would fix it. So:
$table->unsignedBigInteger('module_id')->nullable();
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 am getting the following error when i try and insert something into my database:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
(`database_name`.`catalog_category_entity_varchar`, CONSTRAINT `FK_CAT_CTGR_ENTT_VCHR_ENTT_ID_CAT_CTGR_ENTT_ENTT_ID` FOREIGN KEY (`entity_id`) REFERENCES `catalog_category_entity` (`entity_i)'
Am i right in thinking this means:
there is an entity_i in catalog_category_entity that is not found in
entity_id in catalog_category_entity_varchar
Is there an SQL query i can do on the database that can find what is causing this?
The database is Magentos and this error comes up simply when i create a category programmatically:
$category = Mage::getModel('catalog/category');
$category->setName($design);
$category->setPath($path);
$category->save();
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;