我创建了迁移文件来管理新项目的菜单项。这是迁移文件。
Schema::create('menu_items', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('menugroup_id',10);
$table->string('name',100)->nullable();
$table->enum('type', ['page_link','internal_link','external_link'])->nullable();
$table->string('link')->nullable();
$table->integer('page_id')->nullable();
$table->integer('parent_id')->nullable();
$table->enum('active',['1','0'])->nullable();
$table->integer('lft')->nullable();
$table->integer('rgt')->nullable();
$table->integer('depth')->nullable();
$table->timestamps();
$table->foreign('menu_group_id')->references('id')->on('menu_groups')->onDelete('cascade');
});
在运行迁移命令时发生以下错误。
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key (SQL: create table `menu_items` (`id` int unsigned not null auto_increment primary key, `menugroup_id` int unsigned not null auto_increment primary key, `name` varchar(100) null, `type` enum('page_link', 'internal_link', 'external_link') null, `link` varchar(255) null, `page_id` int null, `parent_id` int null, `active` enum('1', '0') null, `lft` int null, `rgt` int null, `depth` int null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)
at /var/www/html/xyz/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
我无法理解为什么会出现此错误,因为我没有设置除id之外的任何自动增量字段。如果帖子是重复的,我很乐意将其删除。
答案 0 :(得分:1)
变化:
$table->unsignedInteger('menugroup_id',10);
要:
$table->unsignedInteger('menu_group_id',10);
答案 1 :(得分:1)
变化:
$table->unsignedInteger('menugroup_id',10);
要:
$table->unsignedInteger('menugroup_id');
文档告诉第二个变量是打开/关闭auto_increment,因为你已经使用$table->increments('id');
它会中断。
https://laravel.com/api/5.6/Illuminate/Database/Schema/Blueprint.html#method_unsignedInteger
<强>更新强>
当您创建字段并尝试使用menugroup_id
menu_group_id