【问题标题】:Laravel Schema for PostgreSQL SequencePostgreSQL 序列的 Laravel 模式
【发布时间】:2015-05-12 22:27:45
【问题描述】:

这些是我的要求:

  1. 我有一张桌子(upload_training_files)
  2. 字段为idorganisation_id(外)、year_id(外)、created_bycreated_atupdated_byupdated_atfile_path
  3. organisation_id 字段是指organisationsid 字段。它应该是auto_incremented,并且应该用一个序列表(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');
    }
}

这是数据库表的快照

【问题讨论】:

    标签: postgresql laravel schema sequence


    【解决方案1】:

    解决了。 :)

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

    【讨论】:

    • dropPrimary 实际上正在删除序列。谢谢你。
    猜你喜欢
    • 1970-01-01
    • 2012-09-16
    • 2013-03-16
    • 1970-01-01
    • 2015-01-31
    • 2019-07-28
    • 2013-03-24
    • 2021-12-26
    • 1970-01-01
    相关资源
    最近更新 更多