Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7853805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:48:34+00:00 2026-06-02T19:48:34+00:00

I am getting a headache with PHPUnit’s behavior of always running TRUNCATE prior to

  • 0

I am getting a headache with PHPUnit’s behavior of always running TRUNCATE prior to inserting fixtures without first setting foreign key checks off:

Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint

Basically, PHPUnit tries to truncate a table before it inserts fixtures. How do I tell it to SET FOREIGN_KEY_CHECKS=0;?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:48:35+00:00Added an answer on June 2, 2026 at 7:48 pm

    I found the answer it seems. I ended up overriding some methods by extending a class.

    <?php
    
    /**
     * Disables foreign key checks temporarily.
     */
    class TruncateOperation extends \PHPUnit_Extensions_Database_Operation_Truncate
    {
        public function execute(\PHPUnit_Extensions_Database_DB_IDatabaseConnection $connection, \PHPUnit_Extensions_Database_DataSet_IDataSet $dataSet)
        {
            $connection->getConnection()->query("SET foreign_key_checks = 0");
            parent::execute($connection, $dataSet);
            $connection->getConnection()->query("SET foreign_key_checks = 1");
        }
    }
    

    Then example usage:

    class FooTest extends \PHPUnit_Extensions_Database_TestCase
    {
        public function getSetUpOperation()
        {
            $cascadeTruncates = true; // If you want cascading truncates, false otherwise. If unsure choose false.
    
            return new \PHPUnit_Extensions_Database_Operation_Composite(array(
                new TruncateOperation($cascadeTruncates),
                \PHPUnit_Extensions_Database_Operation_Factory::INSERT()
            ));
        }
    }
    

    So I’m effectively disabling foreign key checks and setting them back if they were ever set. Obviously you should make a base class that has this functionality and you extend it rather than PHPUnit’s TestCase.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting into django and this is getting me a headache. I'm trying to
Getting the subdomain from a URL sounds easy at first. http://www.domain.example Scan for the
I'm getting a bit of a headache trying to figure out how to organise
I'm a mod_rewrite noob and I'm getting a headache trying to figure out something
When TransactionScope first came out, I ran into some serious issues getting it to
Hi I am getting a headache with the visualforce repeater control: <apex:repeat value={!productDetails} var=o>
Setting up Eclipse on each machine I work it a real headache and I
I'm getting headache to join my table. Currently I used this way <?php $result
I am getting a headache with standard socket class of .NET framework. Could anybody
I'm getting an insane headache because of this... I'm trying to log into this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.