PostgreSQL 序列的 Laravel 架构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28981423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:07:01  来源:igfitidea点击:

Laravel Schema for PostgreSQL Sequence

postgresqllaravelschemasequence

提问by Nasif Md. Tanjim

These are my requirements:

这些是我的要求:

  1. I have a table(upload_training_files)
  2. The fields are id, organisation_id(foreign), year_id(foreign), created_by, created_at, updated_by, updated_at, file_path.
  3. The organisation_idfield refers to idfield of organisations. It should be auto_incrementedand also should identify with a sequence table(upload_training_file_organisation_id_fk_seq).
  1. 我有一张桌子( upload_training_files)
  2. 字段是id, organisation_id(foreign), year_id(foreign), created_by, created_at, updated_by, updated_at, file_path
  3. organisation_id字段是指 的id字段organisations。它应该auto_incremented并且也应该用一个序列表(upload_training_file_organisation_id_fk_seq)来标识。

I have failed to do this in Laravel after repeated attempts. This is my schema:

经过反复尝试,我在 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');
    }
}

Here's the snapshot of the database table enter image description here

这是数据库表的快照 在此处输入图片说明

采纳答案by Nasif Md. Tanjim

Solved it.:)

解决了。:)

You have to drop the primary keys before assigning every new auto incrementfield.

在分配每个新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');
    }

}