我无法创建多对多关系的记录 “CourseCustomer”表。我收到错误:
SQLSTATE[01000]: Warning: 1265 Data truncated for column 'id' at row 1
INSERT INTO
`course_customers` (
`customer_id`,
`course_id`,
`name`,
`id`,
`updated_at`,
`created_at`
)
VALUES
(
9b39e99d - a89b - 43dc - 9d47 - c5942f2392b0,
9b39efe9 - c506 -4408 - a7db - 22351d18a1da,
certifications / e35w2V6atNcmoG2KDKNsXGJ6o0Dxk7Z1jBKOPTRi.pdf,
9b3d2ce0 - cce6 - 4b9a - 901b - 10b57b275851,
2024 -02 -02 10: 41: 22,
2024 -02 -02 10: 41: 22
)
这是我使用 ids 在课程和客户之间创建链接,然后将certificate_link 保存在“名称”列中的逻辑
function add_new_cert(Request $request){
$validation = Validator::make($request->all(),[
'customer' => 'required|string|min:2|max:100|exists:customers,id',
'course' => 'required|string|max:100|exists:courses,id',
'file' => 'required|file'
]);
if($validation->fails()){
return redirect(route("allCert"))->withErrors($validation);
}
$filename = $request->file('file')->store('certifications');
CourseCustomer::create([
'customer_id'=>$request->customer,
'course_id'=>$request->course,
'name'=> $filename
]);
return redirect(route("customer"));
}
客户表架构
Schema::create('customers', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->string('email');
$table->timestamps();});
课程表架构
Schema::create('courses', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->timestamps();});
CourseCustomer 表架构
Schema::create('course_customers', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->foreignUuid('course_id')->constrained('courses');
$table->foreignUuid('customer_id')->constrained('customers');
$table->timestamps();});
客户模型
class Customer extends Model
{
use HasFactory;
use HasUuids;
public function customers(){
return $this->belongsToMany(Customer::class,'course_customer');
}
}
课程模型
class Course extends Model
{
use HasFactory;
use HasUuids;
public function courses(){
return $this->belongsToMany(Course::class,'course_customer');
}
}
课程客户模型
class CourseCustomer extends Model
{
use HasFactory;
use HasUuids;
protected $fillable = [
'customer_id',
'course_id',
'name',
];
}
id
表中course_customers
列的实际数据类型与您尝试插入的值之间似乎不匹配。错误消息“列 'id' 的数据被截断”表示您尝试插入到 id
列的值被截断或不适合该列的数据类型。
在您的
course_customers
表模式中,您使用的是 Laravel 提供的默认 id
列,它是一个自动递增的整数。但是,在您的 CourseCustomer
模型中,您正在使用 HasUuids
特征,这表明您正在尝试对 id
列使用 UUID。
更改
id
列类型:
更新 course_customers
表架构以对 id
列使用 UUID 数据类型。您可以使用 Laravel 提供的 uuid
方法。
Schema::create('course_customers', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->foreignUuid('course_id')->constrained('courses');
$table->foreignUuid('customer_id')->constrained('customers');
$table->timestamps();
});
删除
HasUuids
特征:
从 HasUuids
模型中删除 CourseCustomer
特征,因为 id
列现在是 UUID。
class CourseCustomer extends Model
{
use HasFactory;
// Remove HasUuids trait
protected $fillable = [
'customer_id',
'course_id',
'name',
];
}
更新迁移: 如果您已经对
course_customers
表运行了迁移,您可能需要回滚并重新运行迁移。
php artisan migrate:rollback
php artisan migrate
进行这些更改后,尝试创建一条新记录,它应该不会出现“列‘id’的数据被截断”错误。这可确保
id
表中的 course_customers
列与 UUID 数据类型兼容。