【问题标题】:(kohana) Unittest DbUnit foreign key constraint(kohana) Unittest DbUnit 外键约束
【发布时间】:2013-12-17 11:02:17
【问题描述】:

我已通过 google/stackoverflow 搜索解决方案,但未能找到令人满意的解决方案。

我的问题:

[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(我使用的)的订单。

这不起作用。

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

mysql.log 中的结果

Connect         root@localhost on integration_db
Query           set foreign_key_checks=0
Connect         root@localhost on integration_db
Query           TRUNCATE `table_name`
Quit
Quit

这是我的 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 上运行

【问题讨论】:

  • 这是 MySQL 的一个限制,据我所知,您只能通过删除并重新创建外键来解决。

标签: mysql kohana dbunit


【解决方案1】:

我的解决方案是保存连接以供多次使用,而不是为每个操作创建新连接。

在您的测试设置函数中添加

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

在 Kohana_Unittest_Database_TestCase 中

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

【讨论】:

    猜你喜欢
    • 2012-04-11
    • 2014-11-24
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2017-04-20
    • 2018-03-05
    相关资源
    最近更新 更多