在Laravel中使用条形码号作为主键

3

我在项目表中遇到了一个问题,无法将条形码号码定义为主键。之前我一直使用普通的ID作为主键。

Schema::create('items', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');            
    $table->integer('price');
}

现在,当我想把id的类型更改为整数条形码号码时,它不起作用。
Schema::create('items', function (Blueprint $table) {
    $table->integer('id', 15)->unsigned;
    $table->string('name');            
    $table->integer('price');
}

运行php artisan migrate后,我遇到了以下错误:
 SQLSTATE[HY000]: General error: 1005 Can't create table `inventory`.`orders` (errno: 150 "Foreign 
 key constraint is incorrectly formed")

下面的订单表(Order table)具有对商品表(Item table)的外键引用。
Schema::create('orders', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('item_id')->unsigned();
    $table->integer('quantity');
    $table->timestamps();
    $table->foreign('item_id')->references('id')->on('items')->onDelete('cascade');
});

你的items表中是否有数据? - TsaiKoga
是的,我想从销售表中访问项目表。 - irham dollah
我在这里删除了phpmyadmin标签,因为这似乎与phpMyAdmin应用程序无关。 - Isaac Bennetch
2个回答

3

试试这个。希望它能够发挥作用。


 Schema::create('items', function (Blueprint $table) {
            $table->bigIncrements('id');


Schema::create('orders', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('quantity');
        $table->timestamps();
        $table->unsignedBigInteger('item_id');
            $table->foreign('item_id')->references('id')->on('items')->onDelete('cascade');
    });


我已经改成了unsigned(),但错误仍然存在。 - irham dollah
请再次检查我的答案,兄弟。 - fahim152

1
尝试更改这个,
    Schema::create('orders', function (Blueprint $table) {
    $table->increments('id');

$table->unsignedBigInteger('item_id');

    $table->integer('quantity');
    $table->timestamps();

    $table->foreign('item_id')->references('id')->on('items')->onDelete('cascade');
});

或者这样怎么样?
 $table->integer('item_id',15)->unsigned();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接