Laravel error Primary Key

1

Good, I'm introducing myself a bit in Laravel and I'm passing a code that I have in pdo, I'm with the creation of tables and tells me this error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your 
MariaDB server version for the right syntax to use near'
num_documento('titulo_documento'
)' at line 1 (SQL: alter table 'documentos' add primary key
'acronimo_proye
cto' using num_documento('titulo_documento'))

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error 
in your SQL syntax; check the manual that corresponds to your MariaDB
server version for the right syntax to use near  num_documento('titulo_documento')' at line 1

And this is my code

public function up()
{
    Schema::create('documentos', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->string('titulo_documento', 50);
        $table->char('acronimo_proyecto', 3);
        $table->string('estado', 10);
        $table->string('idioma', 10);
        $table->char('num_documento', 2);
        $table->char('version', 1);
        $table->char('revision', 1);
        $table->text('descripcion');
        $table->date('fecha');
        $table->char('confidencialidad', 1);
        $table->char('acronimo_documento', 4);
        $table->char('acronimo_subcategorias', 3);
        $table->char('acronimo_usuario', 4);
        $table->char('aprobado', 4);
        $table->char('autorizado', 4);
        $table->char('revisor', 4);
        $table->char('acronimo_empresa', 3);
        $table->string('codigo_proyecto', 30);

        $table->primary('titulo_documento','acronimo_proyecto','num_documento','version','revision','codigo_proyecto');
        $table->foreign('acronimo_subcategorias','acronimo_proyecto')->references('acronimo_subcategorias','acronimo_proyecto')->on('tipo_de_documentos')->onDelete('cascade');
        $table->foreign('acronimo_usuario')->references('acronimo_usuario')->on('usuarios')->onDelete('cascade');
        $table->foreign('aprobado')->references('acronimo_usuario')->on('usuarios')->onDelete('cascade');
        $table->foreign('autorizado')->references('acronimo_usuario')->on('usuarios')->onDelete('cascade');
        $table->foreign('revisor')->references('acronimo_usuario')->on('usuarios')->onDelete('cascade');
        $table->foreign('acronimo_empresa')->references('acronimo')->on('companias')->onDelete('cascade');
        $table->foreign('acronimo_proyecto')->references('acronimo_proyecto')->on('proyectos')->onDelete('cascade');

    });
}

In the foreign I do not believe to have any error, but until not solving the one of the PK I will not be able to continue reviewing the FK, but even so the error comes from the construction of the primary. Thanks to everyone.

    
asked by Alberto Cepero de Andrés 01.06.2017 в 23:59
source

2 answers

0

If you want to have a composite primary key, you must pass an array:

$table->primary(['titulo_documento', 'acronimo_proyecto', 'num_documento', 'version', 'revision', 'codigo_proyecto']);

More information in the documentation: link

    
answered by 02.06.2017 в 00:08
0

If I'm not wrong, you have to pass an array:

  $table->primary(['titulo_documento','acronimo_proyecto','num_documento','version','revision','codigo_proyecto']);

As you put in the documentation, it already indicates that it is a string or an array with the desired columns.

  

Fluent primary (string | array $ columns, string $ name = null) Specify the   primary key (s) for the table.

     

Parameters string | array $ columns string $ name Return Value Fluent

Link: Api schema builder, blueprint

    
answered by 31.05.2018 в 09:19