php - 拉拉维尔 | 当我运行 php artisan migrate 命令时出现 MySQL 错误
问题描述
在简单的汽车租赁预订中使用它并进行手动验证和确认
安装后,添加提供者和发布供应商
迁移数据库时出现此错误:
Illuminate\Database\QueryException : SQLSTATE[42000]: 语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册 f 或在 'after
id
、deleted_at
timestamp null、created_at
timestamp null、updated_a' at line 1 (SQL: create table
services(
idint unsigned not null auto_increment p rimary key,
business_idint unsigned not null,
slugvarchar(255) not null,
namevarchar(255) not null,
持续时间int unsigned not null default '60',
描述附近使用的正确语法varchar(255) not null,
prere quisitesvarchar(255) null,
colorvarchar(12) null,
type_idint unsigned null after
id,
deleted_attimestamp null,
created_attimestamp null,
updated_at` timestamp null) 默认字符集 utf8mb4整理'utf8mb4_unicode_ci')
迁移:
Schema::create('services', function (Blueprint $table) {
$table->increments('id');
$table->integer('business_id')->unsigned();
$table->foreign('business_id')->references('id')->on('businesses')->onDelete('cascade');
$table->string('slug');
$table->string('name');
$table->integer('duration')->unsigned()->default(60);
$table->string('description');
$table->string('prerequisites')->nullable();
$table->string('color', 12)->nullable();
$table->integer('type_id')->unsigned()->nullable()->after('id');
$table->foreign('type_id')->references('id')->on('service_types')->onDelete('cascade');
$table->softDeletes();
$table->Timestamps();
$table->unique(['business_id', 'slug']);
});
解决方案
您调用的方法出现问题
after方法用于列修饰符
示例:当您需要修改表服务时,它将像这样工作。但如果您创建表格,则没有必要。您可以在将属性写入迁移文件时对其进行序列化。
Schema::table('services', function (Blueprint $table) {
$table->integer('type_id')->unsigned()->nullable()->after('id');
});
所以在你的代码中它应该是这样的:
Schema::create('services', function (Blueprint $table) {
$table->increments('id');
$table->integer('type_id')->unsigned()->nullable();
$table->integer('business_id')->unsigned();
$table->foreign('business_id')->references('id')->on('businesses')->onDelete('cascade');
$table->string('slug');
$table->string('name');
$table->integer('duration')->unsigned()->default(60);
$table->string('description');
$table->string('prerequisites')->nullable();
$table->string('color', 12)->nullable();
$table->foreign('type_id')->references('id')->on('service_types')->onDelete('cascade');
$table->softDeletes();
$table->Timestamps();
$table->unique(['business_id', 'slug']);
});
推荐阅读
- c# - 仅显示 WPF DataGrid 中数据库中的重复项
- java - CONNECTIVITY_ACTION 广播接收器不工作
- dbeaver - SELECT AS 隐式转换列
- android - 如何在 Android 中使用 PlaybackParams 保存录音?
- javascript - React js hooks lodash 设置奇怪的行为
- matlab - 如何在matlab中从4个角点创建一个平面?
- sql-server - 使用连接从 3 个表中删除空记录
- javascript - 来自延迟加载组件的 Angular 2+ 访问/更改变量
- javascript - 将绝对定位的粘性元素与兄弟内部的 div 对齐?
- linq - Linq _ 如果一个参数为空则返回空或字符串