Laravel/Lumen 迁移表的表名错误,SQLSTATE[42000]。

6
运行php artisan migrate时,在mysql中尝试创建迁移表时,我遇到了一个错误:
In Connection.php line 664:

  SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name '' (SQL: create table `` (`id` int u  
  nsigned not null auto_increment primary key, `migration` varchar(255) not null, `batch` int not null) default cha  
  racter set utf8 collate 'utf8_unicode_ci')                                                                         


In Connection.php line 452:

  SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name ''  

以下是完整的错误日志:

In Connection.php line 664:

  [Illuminate\Database\QueryException (42000)]                                                                       
  SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name '' (SQL: create table `` (`id` int u  
  nsigned not null auto_increment primary key, `migration` varchar(255) not null, `batch` int not null) default cha  
  racter set utf8 collate 'utf8_unicode_ci')                                                                         


Exception trace:
 () at /home/vagrant/api/vendor/illuminate/database/Connection.php:664
 Illuminate\Database\Connection->runQueryCallback() at /home/vagrant/api/vendor/illuminate/database/Connection.php:624
 Illuminate\Database\Connection->run() at /home/vagrant/api/vendor/illuminate/database/Connection.php:459
 Illuminate\Database\Connection->statement() at /home/vagrant/api/vendor/illuminate/database/Schema/Blueprint.php:97
 Illuminate\Database\Schema\Blueprint->build() at /home/vagrant/api/vendor/illuminate/database/Schema/Builder.php:278
 Illuminate\Database\Schema\Builder->build() at /home/vagrant/api/vendor/illuminate/database/Schema/Builder.php:167
 Illuminate\Database\Schema\Builder->create() at /home/vagrant/api/vendor/illuminate/database/Migrations/DatabaseMigrationRepository.php:157
 Illuminate\Database\Migrations\DatabaseMigrationRepository->createRepository() at /home/vagrant/api/vendor/illuminate/database/Console/Migrations/InstallCommand.php:54
 Illuminate\Database\Console\Migrations\InstallCommand->handle() at n/a:n/a
 call_user_func_array() at /home/vagrant/api/vendor/illuminate/container/BoundMethod.php:32
 Illuminate\Container\BoundMethod::Illuminate\Container\{closure}() at /home/vagrant/api/vendor/illuminate/container/BoundMethod.php:90
 Illuminate\Container\BoundMethod::callBoundMethod() at /home/vagrant/api/vendor/illuminate/container/BoundMethod.php:34
 Illuminate\Container\BoundMethod::call() at /home/vagrant/api/vendor/illuminate/container/Container.php:576
 Illuminate\Container\Container->call() at /home/vagrant/api/vendor/illuminate/console/Command.php:183
 Illuminate\Console\Command->execute() at /home/vagrant/api/vendor/symfony/console/Command/Command.php:255
 Symfony\Component\Console\Command\Command->run() at /home/vagrant/api/vendor/illuminate/console/Command.php:170
 Illuminate\Console\Command->run() at /home/vagrant/api/vendor/illuminate/console/Command.php:198
 Illuminate\Console\Command->call() at /home/vagrant/api/vendor/illuminate/database/Console/Migrations/MigrateCommand.php:93
 Illuminate\Database\Console\Migrations\MigrateCommand->prepareDatabase() at /home/vagrant/api/vendor/illuminate/database/Console/Migrations/MigrateCommand.php:63
 Illuminate\Database\Console\Migrations\MigrateCommand->handle() at n/a:n/a
 call_user_func_array() at /home/vagrant/api/vendor/illuminate/container/BoundMethod.php:32
 Illuminate\Container\BoundMethod::Illuminate\Container\{closure}() at /home/vagrant/api/vendor/illuminate/container/BoundMethod.php:90
 Illuminate\Container\BoundMethod::callBoundMethod() at /home/vagrant/api/vendor/illuminate/container/BoundMethod.php:34
 Illuminate\Container\BoundMethod::call() at /home/vagrant/api/vendor/illuminate/container/Container.php:576
 Illuminate\Container\Container->call() at /home/vagrant/api/vendor/illuminate/console/Command.php:183
 Illuminate\Console\Command->execute() at /home/vagrant/api/vendor/symfony/console/Command/Command.php:255
 Symfony\Component\Console\Command\Command->run() at /home/vagrant/api/vendor/illuminate/console/Command.php:170
 Illuminate\Console\Command->run() at /home/vagrant/api/vendor/symfony/console/Application.php:921
 Symfony\Component\Console\Application->doRunCommand() at /home/vagrant/api/vendor/symfony/console/Application.php:273
 Symfony\Component\Console\Application->doRun() at /home/vagrant/api/vendor/symfony/console/Application.php:149
 Symfony\Component\Console\Application->run() at /home/vagrant/api/vendor/illuminate/console/Application.php:90
 Illuminate\Console\Application->run() at /home/vagrant/api/vendor/laravel/lumen-framework/src/Console/Kernel.php:115
 Laravel\Lumen\Console\Kernel->handle() at /home/vagrant/api/artisan:35

In Connection.php line 452:

  [PDOException (42000)]                                                           
  SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name ''  


Exception trace:
 () at /home/vagrant/api/vendor/illuminate/database/Connection.php:452
 PDO->prepare() at /home/vagrant/api/vendor/illuminate/database/Connection.php:452
 Illuminate\Database\Connection->Illuminate\Database\{closure}() at /home/vagrant/api/vendor/illuminate/database/Connection.php:657
 Illuminate\Database\Connection->runQueryCallback() at /home/vagrant/api/vendor/illuminate/database/Connection.php:624
 Illuminate\Database\Connection->run() at /home/vagrant/api/vendor/illuminate/database/Connection.php:459
 Illuminate\Database\Connection->statement() at /home/vagrant/api/vendor/illuminate/database/Schema/Blueprint.php:97
 Illuminate\Database\Schema\Blueprint->build() at /home/vagrant/api/vendor/illuminate/database/Schema/Builder.php:278
 Illuminate\Database\Schema\Builder->build() at /home/vagrant/api/vendor/illuminate/database/Schema/Builder.php:167
 Illuminate\Database\Schema\Builder->create() at /home/vagrant/api/vendor/illuminate/database/Migrations/DatabaseMigrationRepository.php:157
 Illuminate\Database\Migrations\DatabaseMigrationRepository->createRepository() at /home/vagrant/api/vendor/illuminate/database/Console/Migrations/InstallCommand.php:54
 Illuminate\Database\Console\Migrations\InstallCommand->handle() at n/a:n/a
 call_user_func_array() at /home/vagrant/api/vendor/illuminate/container/BoundMethod.php:32
 Illuminate\Container\BoundMethod::Illuminate\Container\{closure}() at /home/vagrant/api/vendor/illuminate/container/BoundMethod.php:90
 Illuminate\Container\BoundMethod::callBoundMethod() at /home/vagrant/api/vendor/illuminate/container/BoundMethod.php:34
 Illuminate\Container\BoundMethod::call() at /home/vagrant/api/vendor/illuminate/container/Container.php:576
 Illuminate\Container\Container->call() at /home/vagrant/api/vendor/illuminate/console/Command.php:183
 Illuminate\Console\Command->execute() at /home/vagrant/api/vendor/symfony/console/Command/Command.php:255
 Symfony\Component\Console\Command\Command->run() at /home/vagrant/api/vendor/illuminate/console/Command.php:170
 Illuminate\Console\Command->run() at /home/vagrant/api/vendor/illuminate/console/Command.php:198
 Illuminate\Console\Command->call() at /home/vagrant/api/vendor/illuminate/database/Console/Migrations/MigrateCommand.php:93
 Illuminate\Database\Console\Migrations\MigrateCommand->prepareDatabase() at /home/vagrant/api/vendor/illuminate/database/Console/Migrations/MigrateCommand.php:63
 Illuminate\Database\Console\Migrations\MigrateCommand->handle() at n/a:n/a
 call_user_func_array() at /home/vagrant/api/vendor/illuminate/container/BoundMethod.php:32
 Illuminate\Container\BoundMethod::Illuminate\Container\{closure}() at /home/vagrant/api/vendor/illuminate/container/BoundMethod.php:90
 Illuminate\Container\BoundMethod::callBoundMethod() at /home/vagrant/api/vendor/illuminate/container/BoundMethod.php:34
 Illuminate\Container\BoundMethod::call() at /home/vagrant/api/vendor/illuminate/container/Container.php:576
 Illuminate\Container\Container->call() at /home/vagrant/api/vendor/illuminate/console/Command.php:183
 Illuminate\Console\Command->execute() at /home/vagrant/api/vendor/symfony/console/Command/Command.php:255
 Symfony\Component\Console\Command\Command->run() at /home/vagrant/api/vendor/illuminate/console/Command.php:170
 Illuminate\Console\Command->run() at /home/vagrant/api/vendor/symfony/console/Application.php:921
 Symfony\Component\Console\Application->doRunCommand() at /home/vagrant/api/vendor/symfony/console/Application.php:273
 Symfony\Component\Console\Application->doRun() at /home/vagrant/api/vendor/symfony/console/Application.php:149
 Symfony\Component\Console\Application->run() at /home/vagrant/api/vendor/illuminate/console/Application.php:90
 Illuminate\Console\Application->run() at /home/vagrant/api/vendor/laravel/lumen-framework/src/Console/Kernel.php:115
 Laravel\Lumen\Console\Kernel->handle() at /home/vagrant/api/artisan:35

我尝试了销毁和重建 Vagrant 实例,同时删除并重新安装 Composer 包,但问题仍然存在。

如您所请求,迁移文件:

    <?php

    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;

    class CreateTeachersTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('teachers', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('name');
                $table->string('address');
                $table->string('phone');
                $table->string('profession');
                $table->timestamps();
            });
        }

        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('teachers');
        }
    }

    <?php

    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;

    class CreateStudentsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('students', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('name');
                $table->string('address');
                $table->string('phone');
                $table->string('career');
                $table->timestamps();
            });
        }

        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('students');
        }
    }

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCoursesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('courses', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->text('description');
            $table->integer('value')->unsigned();
            $table->bigInteger('teacher_id')->unsigned();
            $table->foreign('teacher_id')->references('id')->on('teachers');
            $table->timestamps();
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('courses');
    }
}

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCourseStudentTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('course_student', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('course_id')->unsigned();
            $table->foreign('course_id')->references('id')->on('courses');
            $table->bigInteger('student_id')->unsigned();
            $table->foreign('student_id')->references('id')->on('students');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('course_student');
    }
}

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('role_id');
            $table->string('email')->unique();
            $table->string('first_name');
            $table->string('last_name');
            $table->string('password');
            $table->tinyInteger('status')->default(1);
            $table->tinyInteger('master')->default(0);
            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateSystemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('systems', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->tinyInteger('status');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('systems');
    }
}

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserRolesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_roles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->bigInteger('system_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('user_roles');
    }
}

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserRolePermissionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_role_permissions', function (Blueprint $table) {
            $table->bigInteger('user_role_id');
            $table->bigInteger('permission_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('user_role_permissions');
    }
}

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserRolePermissionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_role_permissions', function (Blueprint $table) {
            $table->bigInteger('user_role_id');
            $table->bigInteger('permission_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('user_role_permissions');
    }
}

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateLeadTypesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('lead_types', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('branch_id');
            $table->string('name');
            $table->string('token')->nullable();
            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('lead_types');
    }
}

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateLeadStatusesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('lead_statuses', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('branch_id');
            $table->string('name');
            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('lead_statuses');
    }
}

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateLeadsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('leads', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('lead_type_id');
            $table->bigInteger('status_id');
            $table->bigInteger('owner_id');
            $table->string('description');
            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('leads');
    }
}

顺便提一下,我最近添加到系统中的最新软件包是mongodb jenssegers/mongodb,但禁用它并没有产生任何影响。

config/database.php

<?php
return [
    'default' => 'mysql',
    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST'),
            'database' => env('DB_DATABASE'),
            'username' => env('DB_USERNAME'),
            'password' => env('DB_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
        ],
        'mongodb' => [
            'driver' => 'mongodb',
            'host' => env('MONGODB_HOST'),
            'port' => env('MONGODB_PORT'),
            'database' => env('MONGODB_DATABASE'),
            'username' => env('MONGODB_USERNAME'),
            'password' => env('MONGODB_PASSWORD'),
            'options' => [
                'database' => 'admin'
            ]
        ],
    ]
];

请求的.env文件:

APP_NAME=APP
APP_ENV=local
APP_KEY=ka0mdHChmnMoQzSqWle7Zez
APP_DEBUG=true
APP_URL=http://localhost
APP_TIMEZONE=UTC

LOG_CHANNEL=stack
LOG_SLACK_WEBHOOK_URL=

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
QUEUE_CONNECTION=sync

JWT_SECRET=VJXaIerVvIMXAF7w0Tt6FsMU8

1
你在config/database.php里面改了什么吗? - Jerodev
我已经在上面添加了迁移文件和config/database.php文件。 - Paul
@Petay87 添加了 .env 文件。 - Paul
当您的迁移正在执行时,它试图创建一个没有名称的表 - create table `` ,此处表的名称为空。 - Mayank Pandeyz
@MayankPandeyz 是的,它正在尝试创建迁移表,但没有名称。 - Paul
显示剩余5条评论
2个回答

17

经过多次尝试,我发现问题出在config/database.php文件中。我必须添加一个迁移变量('migrations' => 'migrations')

因此,这个:

<?php
return [
    'default' => 'mysql',
    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST'),
            'database' => env('DB_DATABASE'),
            'username' => env('DB_USERNAME'),
            'password' => env('DB_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
        ],
        'mongodb' => [
            'driver' => 'mongodb',
            'host' => env('MONGODB_HOST'),
            'port' => env('MONGODB_PORT'),
            'database' => env('MONGODB_DATABASE'),
            'username' => env('MONGODB_USERNAME'),
            'password' => env('MONGODB_PASSWORD'),
            'options' => [
                'database' => 'admin'
            ]
        ],
    ]
];
变成了这个:
<?php
return [
    'default' => 'mysql',
    'migrations' => 'migrations',
    'connections' => [
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST'),
            'database' => env('DB_DATABASE'),
            'username' => env('DB_USERNAME'),
            'password' => env('DB_PASSWORD'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
        ],
        'mongodb' => [
            'driver' => 'mongodb',
            'host' => env('MONGODB_HOST'),
            'port' => env('MONGODB_PORT'),
            'database' => env('MONGODB_DATABASE'),
            'username' => env('MONGODB_USERNAME'),
            'password' => env('MONGODB_PASSWORD'),
            'options' => [
                'database' => 'admin'
            ]
        ],
    ]
];

现在系统按预期运行。感谢大家的帮助 :)


你好,你知道我该如何在Lumen上实现这个吗? - iamjc015
我创建了自己的连接,添加了一个到另一个数据库的第二个连接,但是我得到的示例也没有指定'migrations'属性,所以我遇到了错误,这个答案为我解决了问题。谢谢 :) - George
@Paul 你是怎么发现这个问题的? - Jared Dunham
兄弟,请把它做成一个合法的答案。感谢你的帮助。 - stormrage
这也是我的问题,谢谢你的提示。 - undefined
显示剩余2条评论

0

在 database.php 中添加 'migrations' => 'migrations',它就可以工作了


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接