在 laravel 迁移中使用 UUID 作为默认值
Using UUID as default value in a laravel migration
我有以下迁移:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpdateRatingGameUserAnswersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('rating_games_user_answers', function (Blueprint $table) {
$table->uuid('answer_token')->default(DB::raw('UUID()'));
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('rating_games_user_answers', function (Blueprint $table) {
$table->dropColumn('answer_token');
});
}
}
如您所见,我正在尝试将 UUID 设置为默认值。我看过了 here
但是当我 运行 php artisan migrate
我看到以下内容:
怎么了?
您不能使用 mySQL 函数作为默认值,您应该设置它或使用触发器。
请像这样尝试:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpdateRatingGameUserAnswersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('rating_games_user_answers', function (Blueprint $table) {
$uuid = DB::raw('select UUID()');
$table->uuid('answer_token')->default($uuid);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('rating_games_user_answers', function (Blueprint $table) {
$table->dropColumn('answer_token');
});
}
}
当我遇到同样的问题时遇到这个问题:
使用 MySQL 8,您 可以 使用函数作为默认值。但是你需要在它们周围加上括号。所以这有效:
$table->uuid('uid')->default(DB::raw('(UUID())'));
但这与 MySQL 5.7 不兼容!
我有以下迁移:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpdateRatingGameUserAnswersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('rating_games_user_answers', function (Blueprint $table) {
$table->uuid('answer_token')->default(DB::raw('UUID()'));
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('rating_games_user_answers', function (Blueprint $table) {
$table->dropColumn('answer_token');
});
}
}
如您所见,我正在尝试将 UUID 设置为默认值。我看过了 here
但是当我 运行 php artisan migrate
我看到以下内容:
怎么了?
您不能使用 mySQL 函数作为默认值,您应该设置它或使用触发器。
请像这样尝试:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpdateRatingGameUserAnswersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('rating_games_user_answers', function (Blueprint $table) {
$uuid = DB::raw('select UUID()');
$table->uuid('answer_token')->default($uuid);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('rating_games_user_answers', function (Blueprint $table) {
$table->dropColumn('answer_token');
});
}
}
当我遇到同样的问题时遇到这个问题: 使用 MySQL 8,您 可以 使用函数作为默认值。但是你需要在它们周围加上括号。所以这有效:
$table->uuid('uid')->default(DB::raw('(UUID())'));
但这与 MySQL 5.7 不兼容!