【问题标题】:Why is CakePHP unable to create fixtures for one controller and returns SQLSTATE[42000] while it is for another controller?为什么 CakePHP 无法为一个控制器创建夹具并返回 SQLSTATE[42000] 而它是为另一个控制器?
【发布时间】:2021-02-07 05:46:57
【问题描述】:

我的 CakePHP 应用程序的快速行(重要部分):

我有一个包含所有需要的用户表(模型、控制器、视图、夹具、测试用例)。 在其他数据中,该用户表引用了一个城市表、一个国家和一个街道表。 对于城市、国家和街道,一切都在那里并且有效:(模型、控制器、视图、固定装置、测试用例)。

Fixtrues 和 Text Cases 是使用 Bake 构建的。

虽然我可以为城市、国家和街道运行测试用例,但我无法为用户运行测试用例,因为我收到以下错误消息:

Exception: Unable to create constraints for fixture "App\Test\Fixture\UsersFixture" in "App\Test\TestCase\Controller\UsersControllerTest" test case: 
SQLSTATE[42000]: Syntax error or access violation: 1142 REFERENCES command denied to user 'cake_user'@'localhost' for table 'cities'
In [/opt/git/premesec/vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureManager.php, line 335]

让我感到困惑的是,当我运行城市 TestCases 时,我可以为城市运行夹具,但是当应该为用户 TestCases 运行相同的夹具时,就会出现问题。

这里是来自 UsersFixtures.php 的约束:

'_constraints' => [
            'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []],
            'city_id' => ['type' => 'foreign', 'columns' => ['city_id'], 'references' => ['cities', 'id'], 'update' => 'noAction', 'delete' => 'noAction', 'length' => []],
            'fk_country_id' => ['type' => 'foreign', 'columns' => ['country_id'], 'references' => ['countries', 'id'], 'update' => 'noAction', 'delete' => 'noAction', 'length' => []],
            'house_number_id' => ['type' => 'foreign', 'columns' => ['house_number_id'], 'references' => ['house_numbers', 'id'], 'update' => 'noAction', 'delete' => 'noAction', 'length' => []],
            'street_id' => ['type' => 'foreign', 'columns' => ['street_id'], 'references' => ['streets', 'id'], 'update' => 'noAction', 'delete' => 'noAction', 'length' => []],
        ],

这里是 CitiesFixtures.php 中的约束:

'_constraints' => [
            'primary' => ['type' => 'primary', 'columns' => ['id'], 'length' => []],
        ],

CakePHP v4.0.6

PHP 版本:7.2.24

mysql Ver 14.14 Distrib 5.7.31,适用于 Linux (x86_64)

【问题讨论】:

    标签: mysql cakephp phpunit fixtures


    【解决方案1】:

    仔细检查错误信息,它实际上与cities 表无关,而是与您的帐户不允许运行的REFERENCES 命令有关,即它是您的Users 夹具中的外键约束,这很可能不会参与您的其他测试,即您的其他测试没有加载Users 夹具。

    长话短说,您需要相应地授予您的cake_user 帐户REFERENCES 权限,这是能够创建外键约束所必需的,或者选择一个已经具有所需权限的其他用户帐户。

    另见

    【讨论】:

      猜你喜欢
      • 2013-07-04
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-14
      • 2013-08-11
      • 2020-10-31
      相关资源
      最近更新 更多