<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAuthorDocumentTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('author_document', function (Blueprint $table) {
            $table->unsignedBigInteger('author_id');
            $table->unsignedBigInteger('document_id');
            $table->unsignedInteger('position')->nullable();
            
            $table->primary(['author_id', 'document_id'], 'author_document_pk');
            
            $table->foreign('author_id')->references('id')
                ->on('authors')->onDelete('CASCADE')->onUpdate('CASCADE');
            
            $table->foreign('document_id')->references('id')
                ->on('documents')->onDelete('CASCADE')->onUpdate('CASCADE');
            
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('author_document');
    }
}
