Laravel PostgreSQL 序列的架构
Laravel Schema for PostgreSQL Sequence
这些是我的要求:
- 我有一个table(
upload_training_files
)
- 字段是
id
, organisation_id
(foreign), year_id
(foreign), created_by
, created_at
, updated_by
, updated_at
, file_path
.
organisation_id
字段引用了organisations
的id
字段。它应该是 auto_incremented
并且还应该用序列 table(upload_training_file_organisation_id_fk_seq
). 来标识
经过多次尝试,我在 Laravel 中未能做到这一点。这是我的架构:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUploadTrainingFileTable extends Migration {
public function up()
{
Schema::create('upload_training_file', function(Blueprint $table) {
$table->bigincrements('id');
$table->biginteger('organisation_id_fk')->unsigned()->unique();
$table->foreign('organisation_id_fk')->references('organisation_id')->on('organisations');
$table->biginteger('year_id_fk')->unsigned()->unique();
$table->foreign('year_id_fk')->references('year_id')->on('year_of_performance');
$table->biginteger('created_by')->nullable();
$table->time('created_at')->nullable();
$table->biginteger('updated_by');
$table->time('updated_at')->nullable();
$table->string('file_path')->nullable();
});
}
public function down()
{
Schema::drop('upload_training_file');
}
}
这是数据库的快照table
解决了。 :)
您必须在分配每个新的 auto increment
字段之前删除主键。
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUploadTrainingFileTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('upload_training_file', function(Blueprint $table) {
$table->bigincrements('upload_training_file_id');
});
Schema::table('upload_training_file', function($table)
{
$table->dropPrimary('upload_training_file_upload_training_file_id_primary');
});
Schema::table('upload_training_file', function($table)
{
$table->bigincrements('organisation_id_fk')->unsigned()->after('id');;
$table->foreign('organisation_id_fk')->references('organisation_id')->on('organisation');
});
Schema::table('upload_training_file', function($table)
{
$table->dropPrimary('upload_training_file_organisation_id_fk_primary');
});
Schema::table('upload_training_file', function($table)
{
$table->bigincrements('year_id_fk')->unsigned()->after('organisation_id_fk');;
$table->foreign('year_id_fk')->references('year_id')->on('year_of_performance');
$table->biginteger('created_by')->nullable();
$table->time('create_date')->nullable();
$table->biginteger('updated_by')->nullable;
$table->time('update_date')->nullable();
$table->string('file_path')->nullable();
});
Schema::table('upload_training_file', function($table)
{
$table->dropPrimary('upload_training_file_year_id_fk_primary');
$table->primary('upload_training_file_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('upload_training_file');
}
}
这些是我的要求:
- 我有一个table(
upload_training_files
) - 字段是
id
,organisation_id
(foreign),year_id
(foreign),created_by
,created_at
,updated_by
,updated_at
,file_path
. organisation_id
字段引用了organisations
的id
字段。它应该是auto_incremented
并且还应该用序列 table(upload_training_file_organisation_id_fk_seq
). 来标识
经过多次尝试,我在 Laravel 中未能做到这一点。这是我的架构:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUploadTrainingFileTable extends Migration {
public function up()
{
Schema::create('upload_training_file', function(Blueprint $table) {
$table->bigincrements('id');
$table->biginteger('organisation_id_fk')->unsigned()->unique();
$table->foreign('organisation_id_fk')->references('organisation_id')->on('organisations');
$table->biginteger('year_id_fk')->unsigned()->unique();
$table->foreign('year_id_fk')->references('year_id')->on('year_of_performance');
$table->biginteger('created_by')->nullable();
$table->time('created_at')->nullable();
$table->biginteger('updated_by');
$table->time('updated_at')->nullable();
$table->string('file_path')->nullable();
});
}
public function down()
{
Schema::drop('upload_training_file');
}
}
这是数据库的快照table
解决了。 :)
您必须在分配每个新的 auto increment
字段之前删除主键。
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUploadTrainingFileTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('upload_training_file', function(Blueprint $table) {
$table->bigincrements('upload_training_file_id');
});
Schema::table('upload_training_file', function($table)
{
$table->dropPrimary('upload_training_file_upload_training_file_id_primary');
});
Schema::table('upload_training_file', function($table)
{
$table->bigincrements('organisation_id_fk')->unsigned()->after('id');;
$table->foreign('organisation_id_fk')->references('organisation_id')->on('organisation');
});
Schema::table('upload_training_file', function($table)
{
$table->dropPrimary('upload_training_file_organisation_id_fk_primary');
});
Schema::table('upload_training_file', function($table)
{
$table->bigincrements('year_id_fk')->unsigned()->after('organisation_id_fk');;
$table->foreign('year_id_fk')->references('year_id')->on('year_of_performance');
$table->biginteger('created_by')->nullable();
$table->time('create_date')->nullable();
$table->biginteger('updated_by')->nullable;
$table->time('update_date')->nullable();
$table->string('file_path')->nullable();
});
Schema::table('upload_training_file', function($table)
{
$table->dropPrimary('upload_training_file_year_id_fk_primary');
$table->primary('upload_training_file_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('upload_training_file');
}
}