在现有的表中添加列时出错

8 浏览
0 Comments

在现有的表中添加列时出错

我正在使用laravel 5.3,想在现有表(即course_chapters)中添加列。因此,我通过命令创建了迁移文件。

 php artisan make:migration add_description_to_course_chapters_table --table=course_chapters

迁移文件已被创建,我已添加了一些代码以在该表中添加列,如下:

<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class AddDescriptionToCourseChaptersTable extends Migration { public function up() { Schema::table('course_chapters', function (Blueprint $table) { $table->text('description')->after('title');
           });
       }
       public function down(){
           Schema::table('course_chapters', function (Blueprint $table) {
               $table->dropColumn('description');
           });
       }
   }

然后我运行命令 php artisan migrate,但表没有被创建,取而代之的是我得到了错误。

←[37;41m
   ←[39;49m
←[37;41m  [Illuminate\Database\QueryException]
   ←[39;49m
←[37;41m  SQLSTATE[42000]: Syntax error or access violation: 1064 You have  an error i  ←[39;49m
←[37;41m  n your SQL syntax; check the manual that corresponds to your MariaDB server  ←[39;49m
←[37;41m   version for the right syntax to use near ') default character set    utf 8 col  ←[39;49m
←[37;41m  late utf8_unicode_ci' at line 1 (SQL: create table     `course_chapters` () def  ←[39;49m
←[37;41m  ault character set utf8 collate utf8_unicode_ci)
   ←[39;49m
←[37;41m
   ←[39;49m
←[37;41m
   ←[39;49m
←[37;41m  [Doctrine\DBAL\Driver\PDOException]
   ←[39;49m
←[37;41m  SQLSTATE[42000]: Syntax error or access violation: 1064 You have   an error i  ←[39;49m
←[37;41m  n your SQL syntax; check the manual that corresponds to your   MariaDB server  ←[39;49m
←[37;41m   version for the right syntax to use near ') default character set   utf 8 col  ←[39;49m
←[37;41m  late utf8_unicode_ci' at line 1
←[37;41m
   ←[39;49m
←[37;41m  [PDOException]
   ←[39;49m
←[37;41m  SQLSTATE[42000]: Syntax error or access violation: 1064 You have   an error i  ←[39;49m
←[37;41m  n your SQL syntax; check the manual that corresponds to your     MariaDB server  ←[39;49m
←[37;41m   version for the right syntax to use near ') default character set    utf 8 col  ←[39;49m
←[37;41m  late utf8_unicode_ci' at line 1
   ←[39;49m
←[37;41m
   ←[39;49m

有什么解决办法吗?我的现有表的迁移文件是

<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CourseChapters extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('course_chapters', function (Blueprint $table) { $table->increments('id');
           $table->integer('course_id');
           $table->integer('chapter_id');
           $table->string('title', 80);
           $table->string('source');
           $table->string('source_type');
           $table->string('max_attempts');
           $table->tinyInteger('status');
           $table->timestamps();
       });
    }
   /**
    * Reverse the migrations.
    *
    * @return void
    */
    public function down()
    {
        Schema::drop('course_chapters');
    }
 }

admin 更改状态以发布 2023年5月22日
0
0 Comments

你做得很好。

要修改/添加现有数据库中的列,必须安装doctrine/dbal,就像您已经安装的那样。但是,如果您查看->after()方法,您会发现它仅在MySQL数据库中起作用。请查看Laravel 5.3文档中的此部分。

我认为您的问题是正在使用MariaDB

0