【问题标题】:Error while adding column in existing table在现有表中添加列时出错
【发布时间】:2017-06-28 08:48:04
【问题描述】:

我在 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');
    }
 }

【问题讨论】:

  • 请添加您在帖子中遇到的错误。以便我们能够更快地为您提供帮助。
  • 我编辑了我的帖子@AbhilekhSingh
  • 能否附上现有表的迁移文件或架构?
  • 我在我的帖子@AdityaSingh 中附加了现有的迁移文件
  • the table was not created 还说您的表已创建并且您要添加新列对吗?

标签: mysql laravel migration


【解决方案1】:

你做得很好。

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

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

【讨论】:

  • 那我该怎么办
  • 不要使用-&gt;after()方法。
  • 代替after()可以用什么
  • 什么都别用,你的列会被添加到表的末尾。
  • 我认为问题出在 dbal/doctorine 软件包上,请告诉我恢复已安装软件包的命令
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-21
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
  • 2010-09-07
相关资源
最近更新 更多