【问题标题】:Laravel SQL Can't create table [duplicate]Laravel SQL无法创建表[重复]
【发布时间】:2015-02-04 19:30:32
【问题描述】:

我正在使用框架 Laravel,我正在尝试将一些文件迁移到数据库(phpmyadmin),但出现此错误: [照亮\数据库\查询异常] SQLSTATE[HY000]: 一般错误: 1005 Can't create table 'loja.#sql-38f_25f' (errno: 150) (SQL: alter table encomenda add c onstraint encomenda_username_foreign 外键 (username) 引用 users (username))

这是我的“推荐表”:

<?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');
}

}

在错误消息中,它说它无法创建表“loja”,但在此文件中没有对“loja”的引用。 我确实想创建一个表'loja',以防万一,这里是代码:

<?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');
}

}

用户表迁移:

<?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');
}

}

现在这里是我的表“linhaItem”迁移:

<?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');
}

}

有人知道怎么回事吗?
编辑:我将 ->onDelete('cascade') 添加到外键,但我得到了同样的错误。
EDIT2:我在 'encomenda' 文件的 id 列中添加了 unsigned,但现在我仍然遇到同样的错误,但使用了 'linhaItem' 表迁移。

【问题讨论】:

  • 在创建 encomenda 之前是否已经创建了 users 表?
  • 请加users表迁移
  • @Kestutis 我添加了 'users' 表和 'linhaItem' 表。我在问题的最后解释了原因
  • 您以什么顺序运行这些迁移?
  • 我正在使用命令 php artisan migrate。有没有办法迁移特定文件?

标签: php mysql sql laravel


【解决方案1】:

你有几个问题:

  1. users迁移:

    $table-&gt;unique('username'); 语句用于创建唯一索引,而不是列:http://laravel.com/docs/4.2/schema#adding-indexes

    将其更改为:$table-&gt;string('username')-&gt;unique(); - 这将为它添加一个列和唯一索引。

  2. encomenda迁移:

    $table-&gt;string('username')-&gt;unsigned();: unsigned 仅用于整数,不能使字符串无符号。

    使用与users迁移相同的代码:$table-&gt;string('username')-&gt;unique();

【讨论】:

  • 感谢您的提示。不幸的是,它仍然没有解决我的问题:/ 我会更新代码
【解决方案2】:

我解决了这个问题。问题是我正在尝试迁移表,这些表具有尚未迁移的表的外键。我只是重命名了所有文件以将它们按可以工作的顺序排列。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-17
    • 2020-04-18
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多