我在使用Laravel 5中的迁移和postgresql时遇到问题。这是在我对迁移文件执行任何操作之前执行表存在检查时。
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//@todo this is not work!
if (Schema::hasTable($this->personSchema . 'person')) {
Schema::table($this->personSchema . 'person', function (Blueprint $table) {
$table->dropForeign('person_person_user_id_foreign');
});
}
Schema::drop($this->accountsSchema . 'users');
PGSchema::drop($this->accountsSchemaName);
}
某些检查不起作用,并且未执行该操作。
你能帮帮我吗? 谢谢!答案 0 :(得分:1)
尝试将配置设置添加到此表单:
/* ... */
'driver' => getenv('DB_CONNECTION'),
'host' => getenv('DB_HOST'),
'database' => getenv('DB_DATABASE'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'schema' => 'testing',
'strict' => true,
/* ... */
我想问题是你使用“$ this-> schema”,而Laravel正试图找到一个名为“schema.scape”的表,而不是“schema”。“scape”,因此,并发生错误。 尝试方案仅在配置文件中指定或在极端情况下,尝试使用DB :: raw($ this-> schema。'。scape')
例如:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTableTestscape extends Migration
{
private $schema = "testing";
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
\Pacuna\Schemas\Facades\PGSchema::create($this->schema);
if (!Schema::hasTable('scape')) {
echo 'Creating table than not exists' . "\n";
Schema::create('scape', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestampsTz();
});
}
if (Schema::hasTable('scape')) {
Schema::table('scape', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
});
echo 'Altering table and adding reference of foreign key' . "\n";
Schema::table('scape', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('accounts.users');
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
echo 'Deleting at all data' . "\n";
Schema::dropIfExists('scape');
\Pacuna\Schemas\Facades\PGSchema::drop($this->schema);
}
}
Laravel将从配置中获取该方案并将其添加到所需的表中。 源代码Laravel为PostgreSQL提供了以下代码:
public function hasTable($table)
{
$sql = $this->grammar->compileTableExists();
$schema = $this->connection->getConfig('schema');
if (is_array($schema)) {
$schema = head($schema);
}
$table = $this->connection->getTablePrefix().$table;
return count($this->connection->select($sql, [$schema, $table])) > 0;
}
答案 1 :(得分:0)
我的朋友@Самуил-Клад,我将如何使用迁移文件进行尝试。
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTableTestscape extends Migration
{
private $schema = "testing";
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
\Pacuna\Schemas\Facades\PGSchema::create($this->schema);
if (!Schema::hasTable($this->schema . '.scape')) {
echo 'Creating table than not exists' . "\n";
Schema::create($this->schema . '.scape', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestampsTz();
});
}
if (Schema::hasTable($this->schema . '.scape')) {
echo 'Creating user_id foreign key' . "\n";
Schema::table($this->schema . '.scape', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
});
echo 'Altering table and adding reference of foreign key' . "\n";
Schema::table($this->schema . '.scape', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('accounts.users');
});
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
echo 'Deleting at all data' . "\n";
Schema::dropIfExists($this->schema . '.scape');
\Pacuna\Schemas\Facades\PGSchema::drop($this->schema);
}
}
但未创建user_id
列。
发生了什么事?
下面有截图。