2019独角兽企业重金招聘Python工程师标准>>>

I've been following a BDD approach using Behat to develop a RESTful API on my current project and wanted to fully isolate the data base from feature to feature, so first I tried with:

/*** @BeforeFeature*/
public static function beforeFeature(BeforeFeatureScope $scope)
{echo shell_exec('app/console doctrine:schema:drop --env=test --force');echo shell_exec('app/console doctrine:schema:create --env=test');echo shell_exec('app/console doctrine:fixtures:load --env=test -n');
}

and it was pretty slow, in my case it was taking an average of 1,5s...

Reading the DoctrineFixturesBundle help I found a nice --purge-with-truncate flag, but TA DAN, it was failing with this issue: https://github.com/doctrine/data-fixtures/pull/127

Well, I solved it by replacing the MySqlPlatform::getTruncateTableSQL method like:

config_test.yml

doctrine: dbal: driver_class: Your\OwnBundle\DBAL\Driver ...

src/Your/OwnBundle/DBAL/Driver.php

<?php

namespace Your\OwnBundle\DBAL;use Doctrine\DBAL\Driver\PDOMySql\Driver as BaseDriver;class Driver extends BaseDriver
{/*** {@inheritdoc}*/public function getDatabasePlatform(){return new Platform();}
}

src/Your/OwnBundle/DBAL/Platform.php

<?php

namespace Your\OwnBundle\DBAL;use Doctrine\DBAL\Platforms\MySqlPlatform;class Platform extends MySqlPlatform
{/*** {@inheritdoc}*/public function getTruncateTableSQL($tableName, $cascade = false){return sprintf('SET foreign_key_checks = 0;TRUNCATE %s;SET foreign_key_checks = 1;', $tableName);}
}

And finally:

/*** @BeforeFeature*/
public static function beforeFeature(BeforeFeatureScope $scope)
{echo shell_exec('app/console doctrine:fixtures:load --env=test --purge-with-truncate -n');
}

It's hacky, I know, but it does the job for the tests and cuts down the average time to 0,4s.

转载于:https://my.oschina.net/u/144160/blog/787102

Workaround for 1701 Cannot truncate a table referenced in a foreign key constraint using doctrine:相关推荐

  1. Cannot truncate a table referenced in a foreign key constraint (alerting.datapoints, CONSTRAINT data

    在truncate表的时候报告以下错误,意思是有的表有外键关联,不能清空. SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot ...

  2. mysql建表Rferencing column ‘‘ and referenced column ‘ in foreign key constraint ‘‘ are incompatible.

    今天建表时遇到了下面的字段类型不一致报错: Referencing column '' and referenced column ' in foreign key constraint '' are ...

  3. The ALTER TABLE statement conflicted with the FOREIGN KEY constraint FK_SortId.

    在创建外键时,出错. USE E_Market GO ALTER TABLE CommodityInfo ADD CONSTRAINT FK_SortId FOREIGN KEY(SortId) re ...

  4. Referencing column and referenced column in foneign key constraint are incompatible报错问题

    [HY000][3780] Referencing column 'dept_id' and referenced column 'id' in foreign key constraint 'dep ...

  5. Truncate a table

    TRUNCATE TABLE hr.emp; Truncate a table deletes all rows in a table and releases used space. Corresp ...

  6. hive Cannot truncate non-managed table

    问题描述 我希望清空一个hive表中的数据,然后报错 Cannot truncate non-managed table 问题分析 由其中的关键字可以看出该表的数据不被hive管理(外表)所以就无法t ...

  7. warning.js:7 Warning: [antdv: Table] Each record in dataSource of table should have a unique `key`

    warning.js:7 Warning: [antdv: Table] Each record in dataSource of table should have a unique key

  8. SQL SERVER | 错误处理 INSERT 语句与 FOREIGN KEY SAME TABLE 约束“FK__Course__Cpno__276EDEB3“冲突。(补充)

    项目平台: SQL SERVER 2019 问题描述: 插入语句时受约束,报错: 消息 547,级别 16,状态 0,第 42 行 INSERT 语句与 FOREIGN KEY SAME TABLE ...

  9. ALTER TABLE 语句与 FOREIGN KEY 约束“FK_Booking_Hotel“冲突。

    错误:SQL Server 创建数据库表的外键时出错 ALTER TABLE 语句与 FOREIGN KEY 约束"FK_Booking_Hotel"冲突.该冲突发生于数据库&qu ...

最新文章

  1. 并发环境下HashMap引起的full gc排查
  2. C++类对象的拷贝构造函数(转载)
  3. docker——kafka-manager安装
  4. 深入理解Fabric环境搭建的详细过程
  5. 10万人的大场馆如何“画座位”?
  6. MySQL等值连接的介绍
  7. ECMAScript 6入门 - 变量的解构赋值
  8. leetcode —— 1013. 将数组分成和相等的三个部分
  9. JAVA入门[1]--安装JDK
  10. LKCOS:基于程序移植的COS系统
  11. PHP中的数组(排序)
  12. 遥感原理与应用_遥感原理与应用考试题库及答案
  13. stm32伺服电机转速计算方法
  14. 免费使用谷歌云服务器一年
  15. Boot Loader点点滴滴(转)
  16. “有些时候,想要赢得天才的尊重,就只能违抗他” |【经纬低调分享】
  17. CAD中用多重插入块加密的图纸怎么分解?
  18. 看萧井陌直播写代码有感
  19. 一图说明APQP的过程和五大质量管理工具的关系
  20. Jmeter-PerfMon Metrics Collector监控解析

热门文章

  1. oracle rac安装
  2. SQL Server 2008 Analysis Services 多维数据库一步一步从入门到精通
  3. asp.net 在使用Response.Redirect try{}catch{}块失效
  4. java修改jar包
  5. ADT启动SDK Manager时一闪而过
  6. [转]Linux 微擎系统搭建
  7. selenium+python自动化84-chrome手机wap模式
  8. 网络之家交换正文 PVST 与PVST+的区别
  9. JWT简介json web token bear token
  10. R语言编程艺术(1)快速入门