运行 迁移 Lumen/Laravel 时出现 PostgreSQL 错误
PostgreSQL error when run migration Lumen/Laravel
我最近更改了我的应用程序连接到我的 PostgreSQL 数据库的方式以添加 read/write 原则。从那时起,当我启动迁移时,出现以下错误:
In Connection.php line 463:
[PDOException (42601)]
SQLSTATE[42601]: Syntax error: 7 ERROR: zero-length delimited identifier at or near """"
LINE 1: create table "" ("id" serial primary key not null, "migratio...
^
当我删除 database.php 文件时,我的迁移工作正常。
我的.env
DB_CONNECTION=pgsql
DB_HOST=192.168.1.1
DB_PORT=5432
DB_DATABASE=database
DB_USERNAME=user
DB_PASSWORD=password
DB_USERNAME_READ=user
DB_PASSWORD_READ=password
我的database.php
<?php
return [
'default' => env('DB_CONNECTION', 'pgsql'),
'connections' => [
'pgsql' => [
'read' => [
'username' => env('DB_USERNAME_READ'),
'password' => env('DB_PASSWORD_READ'),
],
'write' => [
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
],
'sticky' => true,
'driver' => 'pgsql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
],
],
];
和我的一个迁移:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateGameUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('game_user', function (Blueprint $table) {
$table->bigInteger('game_id');
$table->bigInteger('user_id');
$table->foreign('game_id')->references('id')->on('games');
$table->foreign('user_id')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('game_user');
}
}
- PHP 7.3.13
- 流明 6.3.3
- PostgreSQL 9.6.2
Lumen 默认不附带 config/database.php
文件。您可以复制默认的 Laravel 文件,也可以使用自己的设置创建自己的文件。
如果您已经添加了自己的自定义文件,您似乎需要指定迁移 table 名称。
Laravel 的默认条目是 'migrations' => 'migrations'
,当然您可以随意命名。
我在 Laravel 7.x 项目中遇到了同样的问题。
我的解决方案是删除我的应用程序的缓存,然后通过 composer 更新我的依赖项:
php artisan cache:clear
composer update
我最近更改了我的应用程序连接到我的 PostgreSQL 数据库的方式以添加 read/write 原则。从那时起,当我启动迁移时,出现以下错误:
In Connection.php line 463:
[PDOException (42601)]
SQLSTATE[42601]: Syntax error: 7 ERROR: zero-length delimited identifier at or near """"
LINE 1: create table "" ("id" serial primary key not null, "migratio...
^
当我删除 database.php 文件时,我的迁移工作正常。
我的.env
DB_CONNECTION=pgsql
DB_HOST=192.168.1.1
DB_PORT=5432
DB_DATABASE=database
DB_USERNAME=user
DB_PASSWORD=password
DB_USERNAME_READ=user
DB_PASSWORD_READ=password
我的database.php
<?php
return [
'default' => env('DB_CONNECTION', 'pgsql'),
'connections' => [
'pgsql' => [
'read' => [
'username' => env('DB_USERNAME_READ'),
'password' => env('DB_PASSWORD_READ'),
],
'write' => [
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
],
'sticky' => true,
'driver' => 'pgsql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
],
],
];
和我的一个迁移:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateGameUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('game_user', function (Blueprint $table) {
$table->bigInteger('game_id');
$table->bigInteger('user_id');
$table->foreign('game_id')->references('id')->on('games');
$table->foreign('user_id')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('game_user');
}
}
- PHP 7.3.13
- 流明 6.3.3
- PostgreSQL 9.6.2
Lumen 默认不附带 config/database.php
文件。您可以复制默认的 Laravel 文件,也可以使用自己的设置创建自己的文件。
如果您已经添加了自己的自定义文件,您似乎需要指定迁移 table 名称。
Laravel 的默认条目是 'migrations' => 'migrations'
,当然您可以随意命名。
我在 Laravel 7.x 项目中遇到了同样的问题。 我的解决方案是删除我的应用程序的缓存,然后通过 composer 更新我的依赖项:
php artisan cache:clear
composer update