laravel - Laravel 工厂深度嵌套关系失败
问题描述
我遇到了一个问题,即具有深度嵌套关系的工厂无法从 DB 外键错误中正确执行。
我有一系列工厂用于通过应用程序模型将数据传播到数据库中。当我运行所有基础级别(没有关系的工厂)工厂时,我没有任何问题。当我运行所有单级工厂(具有单一关系的工厂)时,我没有任何问题。但是,当我运行任何二级工厂(与另一个有关系的另一个工厂有关系的工厂)时,我会收到数据库错误。
简单来说,这意味着我可以毫无问题地运行ProductKeyFactory
它们ActivationCodeFactory
。这只发生在我尝试运行FeatureActivationFactory
我的工厂
class FeatureActivationFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = FeatureActivation::class;
/** @var string $connection DB Connection */
protected $connection = License::DB;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'code_id' => ActivationCode::factory(),
'productSoftware_id' => ProductSoftware::factory(),
'date' => now()
];
}
}
class ActivationCodeFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = ActivationCode::class;
/** @var string $connection DB Connection */
protected $connection = License::DB;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'key_id' => ProductKey::factory(),
'code' => $this->faker->lexify('??????????'),
'isActive' => 0
];
}
}
class ProductKeyFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = ProductKey::class;
/** @var string $connection DB Connection */
protected $connection = License::DB;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->unique->word,
'startDate' => now(),
'endDate' => $this->faker->dateTimeThisYear()
];
}
}
我的工厂错误FeatureActivation::factory()->create();
:
删除了不正确的错误消息。
编辑
感谢大家指出我的愚蠢错误。是的,这是由于名称重复而导致的错误,但这不是我最终得到的错误。感谢您指出该unique
功能 - 我添加了该功能以避免该错误。请参阅下面的正确错误消息:
Illuminate/Database/QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`license`.`activationCodes`, CONSTRAINT `FK_ActivationCodes_KeyID` FOREIGN KEY (`key_id`) REFERENCES `activationCodes` (`id`) ON DELETE RESTRICT) (SQL: insert into `activationCodes` (`key_id`, `code`, `isActive`) values (18, zvszgdrrlj, 0))'
解决方案
如果你必须处理唯一索引,你需要告诉 Faker 避免生成相同的月份名称。
class ProductKeyFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = ProductKey::class;
/** @var string $connection DB Connection */
protected $connection = License::DB;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->unique()->monthName,
'startDate' => now(),
'endDate' => $this->faker->dateTimeThisYear()
];
}
}
推荐阅读
- powershell - 执行 PowerShell 代码时出错
- blockchain - 与已通过地址部署的以太坊智能合约交互
- android - HTTP 失败:java.net.SocketException:套接字关闭改造
- c++ - 如何获取 Ascii 格式 llvm 的指针类型?
- algorithm - 查看一条线何时接触球体
- python - 如何在Python中连续打印多行文本(一个相邻)
- asp.net - IIS 回收等待什么?
- azure - Azure Bot Service Direct Line API v3.0 在开始对话时不断抛出 403 错误
- php - FCM 在使用 PHP 发送推送通知之前验证设备令牌
- asp.net - 从 elasticsearch index 获取嵌套映射的类型