gpt4 book ai didi

php - 使用 Phinx 添加外键

转载 作者:行者123 更新时间:2023-11-29 01:51:19 26 4
gpt4 key购买 nike

我知道有几篇与此类似的帖子,但它们没有给我所需的答案。因此,我正在使用 Phinx 并尝试添加外键,但出现以下错误:

General error: 1215 Cannot add foreign key constraint

这是带有外键的 php 文件:

<?php

use Phinx\Migration\AbstractMigration;

class CustomQuotes extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('custom_quotes', ['id' => false, 'primary_key' => ['custom_quote_id']]);
$table->addColumn('custom_quote_id', 'biginteger', ['identity' =>true, 'signed' => false])
->addColumn('account_id', 'biginteger', ['limit' => 20, 'null' => true])
->addColumn('quote_type_id', 'integer', ['limit' => 11, 'null' => true])
->addColumn('quote', 'string', ['limit' => 500, 'null' => true])
->addColumn('selected', 'integer', ['limit' => 4, 'null' => true, 'default' => 1])
->addForeignKey('account_id', 'accounts', 'account_id', array('delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'))
->addForeignKey('quote_type_id', 'quote_types', 'quote_type_id', array('delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'))
->create();
}
}

我试图从中获取 id 的两个表是在上表之前创建的。这是另外两个表:

报价类型表

use Phinx\Migration\AbstractMigration;

class CustomQuotes extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('custom_quotes', ['id' => false, 'primary_key' => ['custom_quote_id']]);
$table->addColumn('custom_quote_id', 'biginteger', ['identity' =>true, 'signed' => false])
->addColumn('account_id', 'biginteger', ['limit' => 20, 'null' => true])
->addColumn('quote_type_id', 'integer', ['limit' => 11, 'null' => true])
->addColumn('quote', 'string', ['limit' => 500, 'null' => true])
->addColumn('selected', 'integer', ['limit' => 4, 'null' => true, 'default' => 1])
->addForeignKey('account_id', 'accounts', 'account_id', array('delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'))
->addForeignKey('quote_type_id', 'quote_types', 'quote_type_id', array('delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'))
->create();
}
}

账户表

<?php

use Phinx\Migration\AbstractMigration;

class MakeAccountsTable extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('accounts', ['id' => false, 'primary_key' => ['account_id']]);
$table->addColumn('account_id', 'biginteger', ['identity' =>true, 'signed' => false])
->addColumn('css_user_guid', 'string', ['limit' => 40])
->addColumn('state_id', 'integer', ['limit' => 11, 'null' => true])
->addColumn('phone', 'string', ['limit' => 45, 'null' => true])
->addColumn('street', 'string', ['limit' => 45, 'null' => true])
->addColumn('city', 'string', ['limit' => 45, 'null' => true])
->addColumn('zip', 'string', ['limit' => 45, 'null' => true])
->addColumn('state', 'string', ['limit' => 45, 'null' => true])
->addColumn('terms_of_service_accepted', 'boolean', ['limit' => 1, 'null' => true, 'default' => 0])
->addColumn('quote', 'string', ['limit' => 300, 'null' => true])
->addColumn('date_created', 'datetime', ['null' => true])
->create();
}
}

最佳答案

所以我想通了。事实证明,我必须在 custom_quotes 表中将我的 account_id 和 quote_type_id 设为未签名,因为我在 accounts 和 quote_type 表中将它们设为未签名。现在说得通了。这是解决方案:

<?php

use Phinx\Migration\AbstractMigration;

class CustomQuotes extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* renameColumn
* addIndex
* addForeignKey
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->table('custom_quotes', ['id' => false, 'primary_key' => ['custom_quote_id']]);
$table->addColumn('custom_quote_id', 'biginteger', ['identity' =>true, 'signed' => false])
->addColumn('account_id', 'biginteger', ['limit' => 20, 'null' => true, "signed" => false])
->addColumn('quote_type_id', 'integer', ['limit' => 11, 'null' => true, "signed" => false])
->addColumn('quote', 'string', ['limit' => 500, 'null' => true])
->addColumn('selected', 'integer', ['limit' => 4, 'null' => true, 'default' => 1])
->addForeignKey('account_id', 'accounts', 'account_id', array('delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'))
->addForeignKey('quote_type_id', 'quote_types', 'quote_type_id', array('delete'=> 'NO_ACTION', 'update'=> 'NO_ACTION'))
->create();
}
}

关于php - 使用 Phinx 添加外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41840118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com