[wp-trac] [WordPress Trac] #44351: Fix "Foreign key constraint is incorrectly formed" when running unit tests
WordPress Trac
noreply at wordpress.org
Mon Jun 11 17:09:19 UTC 2018
#44351: Fix "Foreign key constraint is incorrectly formed" when running unit tests
------------------------------+-----------------------------
Reporter: conner_bw | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Build/Test Tools | Version: 4.9.6
Severity: normal | Keywords:
Focuses: |
------------------------------+-----------------------------
I kept getting "Foreign key constraint is incorrectly formed" when running
tests on my plugin. This drove me nuts for a few hours. The reason is
[https://github.com/WordPress/wordpress-
develop/blob/9e38c2847e5f3b94785378094d17f09577215d3c/tests/phpunit/includes/testcase.php#L335
WP_UnitTestCase::start_transaction()]
The WordPress testing framework changes all 'CREATE TABLE' queries to
'CREATE TEMPORARY TABLE'.
[https://dev.mysql.com/doc/refman/5.5/en/create-table-foreign-keys.html
From the MYSQL manual]:
> Foreign key relationships involve a parent table that holds the central
data values, and a child table with identical values pointing back to its
parent. The FOREIGN KEY clause is specified in the child table. The parent
and child tables must use the same storage engine. **They must not be
TEMPORARY tables**.
Please add something like:
{{{#!php
<?php
add_filter( 'query', [ $this, '_alter_temporary_tables' ] );
// ... snip ...
public function _alter_temporary_tables( $query ) {
if ( 'ALTER TABLE' === substr( trim( $query ), 0, 11 ) && strpos(
$query, 'ADD CONSTRAINT' ) !== false && strpos( $query, 'FOREIGN KEY' )
!== false ) {
return 'SELECT 1'; // Replace borken foreign key query with a fake
query
}
return $query;
}
}}}
To `WP_UnitTestCase::start_transaction()`
Thank you for your consideration.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/44351>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list