提问人:666 提问时间:10/30/2023 更新时间:10/30/2023 访问量:42
数据库超出长度限制,即使在运行迁移文件后也是如此
The database exceeds the length limit, even after running the migration files
问:
Laravel版本
10.15.0
PHP版本
8.1
数据库驱动程序和版本
适用于 Linux on x86_64 的 Ver 8.0.33
描述
我的数据库有一个长度为 4 varchar 的“province_code”字段。
尝试保存值“PT-11”时,导致错误。
运行迁移文件并将长度增加到 255 后,错误仍然存在。
检查后,字段长度为 255,并添加了新记录。
迁移文件 :
public function up(): void
{
//
Schema::table('customers', function (Blueprint $table) {
$table->string('country_code',255)->nullable()->comment('国家缩写')->change();
$table->string('province_code',255)->nullable()->comment('省/州缩写')->change();
});
}
消息(使用虚假数据):
"getMessage": "SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'province_code' at row 1 (Connection: mysql, SQL: insert into `customers` (`email`, `root_id`, `team_id`, `name`, `phone`, `country`, `province`, `city`, `province_code`, `address`, `other_address`, `zip_code`, `user_id`, `email_account_id`, `updated_at`, `created_at`) values ([email protected], 0, 0, kdoask 320, +3511932000011, Portugal, Lisboa, djkasld dalsjd dkaskda oewqpoe, PT-11, FDSFSD FDSFDS dsdsfds, lfpslfps dasdad dasdaseq, 220011, ?, 1, 2023-10-30 15:25:39, 2023-10-30 15:25:39))",
"method": "App\\Jobs\\SaveCustomer::handle",
"line": 795
fillable 属性(部分):
protected $fillable = [
'name',
'email',
'address',
'city',
'country',
'country_code',
'province',
'province_code',
该错误正在运行以下代码:
Customer::updateOrCreate(
[
'email' => $shipping['email'],
'root_id' => $this->ticket->root_id,
'team_id' => $this->ticket->team_id,
],
$shipping + [
'root_id' => $this->ticket->root_id,
'team_id' => $this->ticket->team_id,
'user_id' => $this->ticket->user_id,
'email_account_id' => $this->ticket->email_account_id,
]);
重现步骤
- 首次运行 migtate(创建字段,长度为 4)
- 代码已写入(复制标题“The bug is running this code”)的内容)
- 运行 migtate(更改字段,长度为 255)
- 运行代码
我检查了数据库字段信息,发现长度是 255,
它可以在行中插入值“PT-11”。
但是,运行此代码会导致异常,
这意味着它被多次触发。
过去,它被多次触发而没有遇到此问题。
成功是本机 SQL,但错误是运行代码
我不明白这个问题。
答: 暂无答案
评论