2

I'm using the framework Laravel, and I'm trying to migrate some files to the database(phpmyadmin), but I'm getting this error: [Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table 'loja.#sql-38f_25f' (errno: 150) (SQL: alter table encomenda add c onstraint encomenda_username_foreign foreign key (username) references users (username))

This is my table "encomenda":

<?php

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

class CreateEncomendaTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    // Creates the encomenda table
    Schema::create('encomenda', function($table)
    {
        $table->engine = 'InnoDB';
        $table->increments('IDEncomenda')->unsigned();
        $table->integer('id')->unsigned();
        $table->foreign('id')->references('id')->on('users')->onDelete('cascade');
        $table->string('editora');
    });
}

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

}

In the error message, it says that it can't create the table 'loja', but no where in this file is there a reference to 'loja'. I indeed do want to create a table 'loja', and just in case, here's the code:

<?php

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

class CreateLojaTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('loja', function($table)
    {
        $table->engine = 'InnoDB';
        $table->primary('api_key');
    });
}

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

}

Users table migration:

<?php
use Illuminate\Database\Migrations\Migration;

class ConfideSetupUsersTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    // Creates the users table
    Schema::create('users', function($table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id')->unsigned();
        $table->string('username')->unique();
        $table->string('email');
        $table->string('password');
        $table->string('confirmation_code');
        $table->string('remember_token')->nullable();
        $table->boolean('confirmed')->default(false);
        $table->boolean('admin')->default(false);
        $table->timestamps();
    });


    // Creates password reminders table
    Schema::create('password_reminders', function($table)
    {
        $table->engine = 'InnoDB';
        $table->string('email');
        $table->string('token');
        $table->timestamp('created_at');
    });
}

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

}

And now here is my table 'linhaItem' migration:

<?php

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

class CreateLinhaItemTable extends Migration {

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('linhaItem', function($table)
    {
        $table->engine = 'InnoDB';
        $table->increments('IDLinha')->unsigned();
        $table->integer('IDDisco')->unsigned();
        $table->integer('IDCarrinho')->unsigned();
        $table->double('preco');
        $table->integer('quantidade');
        $table->foreign('IDDisco')->references('IDDisco')->on('disco')->onDelete('cascade');
        $table->foreign('IDCarrinho')->references('IDCarrinho')->on('carrinho')-onDelete('cascade');
    });
}

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

}

Anybody know what's wrong?
EDIT: I added the ->onDelete('cascade') to the foreign key, but I get the same error.
EDIT2: I added unsigned to the id column in the 'encomenda' file, but now I'm still getting the same error, but with the 'linhaItem' table migration.

Adam Silva
  • 1,013
  • 18
  • 48

2 Answers2

0

You have a few issues:

  1. users migration:

    $table->unique('username'); statement is for creating a unique index, not a column: http://laravel.com/docs/4.2/schema#adding-indexes

    Change it to this: $table->string('username')->unique(); - this will add a column and unique index for it.

  2. encomenda migration:

    $table->string('username')->unsigned();: unsigned is used only with integers, you can't make a string unsigned.

    Use the same code as in users migration: $table->string('username')->unique();

Kestutis
  • 600
  • 5
  • 13
0

I solved the problem. The thing is I was trying to migrate tables, which had foreign keys to tables that weren't migrated yet. I just renamed all the files to put them in an order which would work.

Adam Silva
  • 1,013
  • 18
  • 48