php - Laravel SQL 无法创建表

标签 php mysql sql laravel

<分区>

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

这是我的表“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');
}

}

在错误消息中,它说它无法创建表“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' 表迁移。

最佳答案

你有几个问题:

  1. 用户迁移:

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

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

  2. 推荐迁移:

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

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

关于php - Laravel SQL 无法创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27336044/

相关文章:

javascript - 发送不带 "?"的 GET 变量

php - 如何在session过期时自动删除一个表的所有记录

php - 脚本在返回 header : index. php 之前超时

php - PHP 脚本中的特殊字符停止执行

SQL 将 INSERT SELECT 也转换为 UPDATE

php - 我如何使用 php 和 codeigniter 将数据插入数据库

javascript - 获取全局 JQuery $(document).ajaxSuccess 函数以在所有浏览器中工作

php - 使用 PHP 在 HTML 页面中显示 SQL 表内容

php - 发布内容后立即从 mysql 显示时间?

sql - 使用内部SELECT查询“清理”任意SQL?