首页 > 解决方案 > 为什么表在数据库中不存在

问题描述

我的代码有什么问题?,迁移成功但表不存在

public function up()
{
    Schema::create('student', function (Blueprint $table) {
        $table->varchar('student_id', 6)->primary_key()->nullable(false);
        $table->varchar('student_name', 50)->nullable(false);
        $table->enum('student_class', ['RPL-1','RPL-2','RPL-3'])->nullable(true);
        $table->enum('student_gender', ['MALE','FEMALE'])->nullable(true);
        $table->text('student_address')->nullable(true);
        $table->tinyinteger('student_status', ['1'])->default('1');
        $table->timestamps();
    });
}

标签: mysqldatabaselaravelmigration

解决方案


由于尝试使用 Laravel 中不可用的功能或滥用这些功能,您的示例中的迁移将无法运行。我已经将一个新的迁移放在一起并对其进行了测试,它运行良好。

Schema::create('student', function (Blueprint $table) {
    $table->string('student_id', 6)->primary_key()->nullable(false);
    $table->string('student_name', 50)->nullable(false);
    $table->enum('student_class', ['RPL-1', 'RPL-2', 'RPL-3'])->nullable(true);
    $table->enum('student_gender', ['MALE', 'FEMALE'])->nullable(true);
    $table->text('student_address')->nullable(true);
    $table->tinyinteger('student_status')->default('1');
    $table->timestamps();
});

我假设student_id需要在您的应用程序中使用某种唯一代码,因此我已将其更改为使用该string功能。这将创建一个 varchar 类型字段,第二个参数是字符串的长度。

我还更改了 的tinyinteger函数参数student_status,看起来您正在尝试指定所有可能的值,类似于您对enum. 该tinyinteger类型不能采用可能值的列表,因此您可能希望将其更改为布尔值(如果状态为真或假)、枚举或相关表的外键(如果相关)。

如果您在应用程序中使用 Eloquent ORM,则可能不需要在所有列名前加上student. 如果使用正确,您应该能够简单地使用所存储变量的名称,例如student_name将成为name.


推荐阅读