如何为 postgresql 数组数据类型创建 laravel 迁移
how to create laravel migration for postgresql array data type
我有以下laravel迁移向上方法
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('mobile')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
// $table->string('sports');
$table->date('dob');
$table->string('height');
$table->rememberToken();
$table->timestamps();
});
DB::statement('ALTER TABLE users ADD COLUMN sports TYPE text[] AFTER password');
}
当我 运行 迁移时,它将显示 SQLSTATE[42601]:语法错误:7 错误:“文本”处或附近的语法错误
LINE 1: ALTER TABLE users ADD COLUMN sports TYPE text[] AFTER passwo...
^(SQL:更改 TABLE 用户添加列体育类型文本[] 在密码之后)。我不知道那里有什么问题?
请尝试这样的 Sql 语句:
DB::statement('alter table users alter sports drop default');
DB::statement('alter table users alter sports type text[] using array[sports]');
DB::statement("alter table users alter sports set default '{}'");
public function up()
{
Schema::create('users', function (Blueprint $table) {
// ...
$table->string('sports');
// ...
$table->timestamps();
});
DB::statement('ALTER TABLE users ALTER COLUMN sports TYPE text[] USING ARRAY[sports]');
}
我有以下laravel迁移向上方法
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('mobile')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
// $table->string('sports');
$table->date('dob');
$table->string('height');
$table->rememberToken();
$table->timestamps();
});
DB::statement('ALTER TABLE users ADD COLUMN sports TYPE text[] AFTER password');
}
当我 运行 迁移时,它将显示 SQLSTATE[42601]:语法错误:7 错误:“文本”处或附近的语法错误 LINE 1: ALTER TABLE users ADD COLUMN sports TYPE text[] AFTER passwo... ^(SQL:更改 TABLE 用户添加列体育类型文本[] 在密码之后)。我不知道那里有什么问题?
请尝试这样的 Sql 语句:
DB::statement('alter table users alter sports drop default');
DB::statement('alter table users alter sports type text[] using array[sports]');
DB::statement("alter table users alter sports set default '{}'");
public function up()
{
Schema::create('users', function (Blueprint $table) {
// ...
$table->string('sports');
// ...
$table->timestamps();
});
DB::statement('ALTER TABLE users ALTER COLUMN sports TYPE text[] USING ARRAY[sports]');
}