我将用户表设置如下
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('password')->nullable();
$table->tinyInteger('status')->unsigned()-> nullable(false) -> change()->default(1);
$table->string('confirmation_code')->nullable();
$table->tinyInteger('confirmed')->unsigned()->nullable(false) -> change()->default(1);
$table->rememberToken();
$table->unsignedInteger('deleted_at');
$table->timestamps();
});
}
但显示如下错误
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name '' (SQL: alter table `` add `status` tinyint unsigned not null default '1' after `password`, add `confirmation_code` varchar(255) null after `status`, add `confirmed` tinyint(1) not null default '1' after `confirmation_code`, add `deleted_at` timestamp null)
at C:\xampp\htdocs\cron\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name ''")
C:\xampp\htdocs\cron\vendor\laravel\framework\src\Illuminate\Database\Connection.php:452
2 PDO::prepare("alter table `` add `status` tinyint unsigned not null default '1' after `password`, add `confirmation_code` varchar(255) null after `status`, add `confirmed` tinyint(1) not null default '1' after `confirmation_code`, add `deleted_at` timestamp null")
C:\xampp\htdocs\cron\vendor\laravel\framework\src\Illuminate\Database\Connection.php:452
请使用参数-v查看更多详细信息。
请帮助我纠正此迁移。 我认为我的错不为空
答案 0 :(得分:1)
您不应该在->change()
中使用Schema::create()
方法,因为它会尝试更改尚未创建的列。要创建允许空值的列,只需执行以下操作:
Schema::create("table", function (Blueprint $table) {
$table->string("column"); // Omit ->nullable() to force `NOT NULL`
$table->string("nullable_column")->nullable();
});
运行此迁移并创建表后,table.column
将初始化为NOT NULL
,而table.nullable_column
将会初始化为NULL
。
现在,如果您需要在以后的迁移中更改这些 ,那就是在使用->nullable(false)->change();
时,就像这样:
Schema::table("table", function (Blueprint $table) {
$table->string("column")->nullable()->change();
$table->string("nullable_column")->nullable(false)->change();
});
此迁移之后,table.column
将允许NULL
,而table.nullable_column
将不允许。