数据库迁移notnull()?
Database migration notnull()?
我使用迁移将 nullable() 添加到列。
class ChangeUgIdCanNull extends Migration
{
public function up()
{
Schema::table('service_request_step', function (Blueprint $table) {
$table->dropForeign(['ug_id']);
});
Schema::table('service_request_step', function (Blueprint $table) {
$table->dropIndex(['ug_id']);
});
Schema::table('service_request_step', function (Blueprint $table) {
$table->integer('ug_id')->unsigned()->index()->nullable()->change();
$table->foreign('ug_id')->references('ug_id')
->on('user_group')->onDelete('cascade');
});
}
public function down()
{
Schema::table('service_request_step', function (Blueprint $table) {
$table->dropForeign(['ug_id']);
});
Schema::table('service_request_step', function (Blueprint $table) {
$table->dropIndex(['ug_id']);
});
Schema::table('service_request_step', function (Blueprint $table) {
$table->integer('ug_id')->unsigned()->index()->change();
$table->foreign('ug_id')->references('ug_id')
->on('user_group')->onDelete('cascade');
});
}
}
当我使用 php artisan migrate
时很好。
但是,当我想 php artisan migrate:rollback.
在我的数据库中 'ug_id' 列仍然可以为空。
我有像 $table->integer('ug_id')->unsigned()->index()->notnull()->change();
这样的功能吗
Laravel Version: 5.4.19
PHP Version: 7.1.3
Database Driver & Version: 10.2.4-MariaDB
您可以使用 nullable(false)
。
您的代码应该是
$table->integer('ug_id')->unsigned()->index()->nullable(false)->change();
我使用迁移将 nullable() 添加到列。
class ChangeUgIdCanNull extends Migration
{
public function up()
{
Schema::table('service_request_step', function (Blueprint $table) {
$table->dropForeign(['ug_id']);
});
Schema::table('service_request_step', function (Blueprint $table) {
$table->dropIndex(['ug_id']);
});
Schema::table('service_request_step', function (Blueprint $table) {
$table->integer('ug_id')->unsigned()->index()->nullable()->change();
$table->foreign('ug_id')->references('ug_id')
->on('user_group')->onDelete('cascade');
});
}
public function down()
{
Schema::table('service_request_step', function (Blueprint $table) {
$table->dropForeign(['ug_id']);
});
Schema::table('service_request_step', function (Blueprint $table) {
$table->dropIndex(['ug_id']);
});
Schema::table('service_request_step', function (Blueprint $table) {
$table->integer('ug_id')->unsigned()->index()->change();
$table->foreign('ug_id')->references('ug_id')
->on('user_group')->onDelete('cascade');
});
}
}
当我使用 php artisan migrate
时很好。
但是,当我想 php artisan migrate:rollback.
在我的数据库中 'ug_id' 列仍然可以为空。
我有像 $table->integer('ug_id')->unsigned()->index()->notnull()->change();
Laravel Version: 5.4.19
PHP Version: 7.1.3
Database Driver & Version: 10.2.4-MariaDB
您可以使用 nullable(false)
。
您的代码应该是
$table->integer('ug_id')->unsigned()->index()->nullable(false)->change();