无法在 Laravel 7 中创建无符号列

Can't create unsigned column in Laravel 7

我正在使用 laravel 构建管理应用程序。我正在尝试在 laravel 中创建表 'role_user',但是当我 运行 'php artisan migrate' 命令时,我得到以下信息:BadMethodCallException 方法 Illuminate\Database\Schema\Blueprint::name 不存在。谁能告诉你这个错误是什么意思?


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

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

但如果我尝试迁移,我会看到

λ php artisan migrate
Migrating: 2020_06_07_055653_create_roles_table

   BadMethodCallException

  Method Illuminate\Database\Schema\Blueprint::name does not exist.

  at E:\laragon\www\os\vendor\laravel\framework\src\Illuminate\Support\Traits\Macroable.php:103
     99|      */
    100|     public function __call($method, $parameters)
    101|     {
    102|         if (! static::hasMacro($method)) {
  > 103|             throw new BadMethodCallException(sprintf(
    104|                 'Method %s::%s does not exist.', static::class, $method
    105|             ));
    106|         }
    107|

  • Bad Method Call: Did you mean Illuminate\Database\Schema\Blueprint::rename() ?

  1   E:\laragon\www\os\database\migrations20_06_07_055653_create_roles_table.php:18
      Illuminate\Database\Schema\Blueprint::__call("name", [])

  2   E:\laragon\www\os\vendor\laravel\framework\src\Illuminate\Database\Schema\Builder.php:166
      CreateRolesTable::{closure}(Object(Illuminate\Database\Schema\Blueprint))

您可以将代码更改为:

    Schema::create('roles', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->timestamps();
    });

改为:

$table->name();

应该是:

$table->string('name', 32); // change 32 to max length

为什么?

MySQL(或任何其他数据库引擎)中没有 "name" type。您可能被 ->id()->timestamps() 误导了。也没有 "id" 列类型,但这是有效的,因为在 Laravel 中,这是作为快捷方式(因为它经常被使用)。

所以如果你使用没有区别:

$table->id().
$table->timestamps();

$table->bigIncrements('id');
$table->timestamp('created_at', $precision)->nullable();
$table->timestamp('updated_at', $precision)->nullable();

因为无论如何它都在做同样的事情。

Read more in docs.

改变

$table->id()

$table->bigIncrements('id');