2013-12-17 7 views
0

솔루션에 대해 google/stackoverflow를 통해 검색했지만 만족스러운 해결책을 찾지 못했습니다.(kohana) Unittest DbUnit 외래 키 제약

내 문제 :

[SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a 
foreign key constraint (integration.b_node_leafs, CONSTRAINT fk_node_id 
FOREIGN KEY (node_id) REFERENCES integration.b_nodes (id))] 

그리고 난 아무데도이 문제도 (내가 사용)에 XmlDataSet에 대한 순서를 해결하기 위해 문서에서 찾을 수 없습니다.

이것은 작동하지 않습니다. (당신은 내가 바닥에서 시작 볼 수 있듯이) mysql.log

Connect   [email protected] on integration_db 
Query   set foreign_key_checks=0 
Connect   [email protected] on integration_db 
Query   TRUNCATE `table_name` 
Quit 
Quit 

에서

public function setUp() { 
    $conn=$this->getConnection(); 
    $conn->getConnection()->query("set foreign_key_checks=0"); 
    parent::setUp(); 
    $conn->getConnection()->query("set foreign_key_checks=1"); 
} 

결과이 내 dataset.xml입니다.

<?xml version="1.0" encoding="UTF-8"?> 
<dataset> 
    <!-- Meta for node --> 
    <b_meta id="4321" created_by="###NULL###" deleted="0" /> 
    <!-- Meta for leaf --> 
    <b_meta id="1010" created_by="###NULL###" deleted="0" /> 
    <!-- Meta for post --> 
    <b_meta id="1050" created_by="###NULL###" deleted="0" /> 
    <!-- meta for comment --> 
    <b_meta id="7894" created_by="###NULL###" deleted="0" /> 

    <!-- Add comment --> 
    <b_comments id="5555" meta_id="7894" text="This is a integrationtest" /> 
    <!-- Add Post --> 
    <b_posts id="4646" meta_id="1050" title="How to integration" seo_title="how-to-integration" text="Explain why to use integrationtests" /> 
    <!-- Link comment to post --> 
    <b_post_comments post_id="4646" comment_id="5555" /> 
    <!-- Add Leaf --> 
    <b_leafs id="3535" meta_id="1010" title="App Testing" seo_title="app-testing" /> 
    <!-- Link leaf to post --> 
    <b_leaf_posts leaf_id="3535" post_id="4646" /> 
    <!-- Add node --> 
    <b_nodes id="1234" meta_id="4321" type="forum" title="PHP" /> 
    <!-- Link node to leaf --> 
    <b_node_leafs node_id="1234" leaf_id="3535" /> 
</dataset> 

PS : 나는 내 솔루션은 다수의 연결 대신 각 작업에 대한 새로운 연결을 생성하는 사용하는 저장했다 phpunit을 3.7.28

+0

내가 아는 한 외래 키를 만드는거야. –

답변

0

에서 실행됩니다. 테스트 설정 기능에서

Kohana_Unittest_Database_TestCase

에서
public function setUp() 
{ 
    $conn = $this->getConnection(); 
    $conn->getConnection()->query("set foreign_key_checks=0"); 
    return parent::setUp(); 
} 

protected $_connection; 
public function getConnection() 
{ 
    if ($this->_connection !== NULL) 
    { 
    return $this->_connection; 
    } 

    // Get the unittesting db connection 
    $config = Kohana::$config->load('database.'.$this->_database_connection); 

    if(strtolower($config['type']) !== 'pdo') 
    { 
    $config['connection']['dsn'] = strtolower($config['type']).':'. 
     'host='.$config['connection']['hostname'].';'. 
     'dbname='.$config['connection']['database']; 
    } 

    $pdo = new PDO(
    $config['connection']['dsn'], 
$config['connection']['username'], 
$config['connection']['password'] 
); 

    $this->_connection = $this->createDefaultDBConnection($pdo, $config['connection']['database']); 

    return $this->_connection; 
} 

내 풀 요청을 참조 추가 : https://github.com/kohana/unittest/pull/36

만 놓는 방법으로 해결하려면 재 수있는 MySQL의 제한이다