【问题标题】:Laravel 6 - SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;Laravel 6 - SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;
【发布时间】:2020-02-15 23:09:29
【问题描述】:

当我跑步时

php artisan migrate

迁移这个Schema

public function up()
{
    Schema::create('transaction_ins', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('idTransactionIN');
        $table->date('buy_date');
        $table->string('id_Supplier')->unsigned();
        $table->string('id_DeviceType')->unsigned();
        $table->string('id_DeviceBrand')->unsigned();
        $table->string('device_name');
        $table->mediumText('device_name');
        $table->decimal('device_price', 11, 2);
        $table->integer('amount');
        $table->decimal('sum_price', 15, 2);
        $table->mediumText('context');
        $table->timestamps();

        //Foreign Key
        $table->foreign('id_Supplier')->references('idSupplier')->on('suppliers')->onUpdate('cascade');
        $table->foreign('id_DeviceType')->references('idDeviceType')->on('device_types')->onUpdate('cascade');
        $table->foreign('id_DeviceBrand')->references('idDevicebrand')->on('device_brands')->onUpdate('cascade');
    });
}

我收到此错误消息

Illuminate\Database\QueryException : SQLSTATE[42000]: 语法错误或 访问冲突:1064 您的 SQL 语法有错误;查看 与您的 MariaDB 服务器版本相对应的手册 在 'unsigned not null, id_DeviceType 附近使用正确的语法 VARCHAR(255)无符号不为空,id_DeviceBra' at line 1 (SQL: create table transaction_ins ( ID bigint unsigned not null auto_increment primary key, idTransactionIN varchar(255) not null,购买日期date not null, id_Supplier varchar(255) unsigned not null, id_DeviceType varchar(255) unsigned not null, id_DeviceBrand varchar(255) unsigned not null, DEVICE_NAME varchar(255) not null, DEVICE_NAME mediumtext not null, device_price decimal(11, 2) not null,amountint not null,sum_pricedecimal(15, 2) not null, contextmediumtext not null,created_attimestamp null, updated_at` timestamp null) 默认字符集 utf8mb4 collat​​e 'utf8mb4_unicode_ci')

【问题讨论】:

  • ids 真的是字符串吗?为什么不是整数?
  • 是的,它的字符串我制作了一个带有字母的自定义 ID。
  • 当我听到 ID 是整数时首先想到的是¯\_(ツ)_/¯ 所以有点奇怪

标签: php mysql laravel laravel-6


【解决方案1】:

你不能有无符号字符串,这意味着整数

只需删除无符号修饰符(如果它们真的是字符串)

$table->string('id_Supplier');
$table->string('id_DeviceType');
$table->string('id_DeviceBrand');

【讨论】:

  • 迁移发生在数据库中创建的表,但随后出现新错误(errno: 150 "Foreign key constraint is incorrectly formed"),如果我的unique id 有类似字母,将数据类型从string 更改为integer 是否安全ABC0001
  • 我得看看你的其他表才能告诉
  • 不,这不安全,整数中不能有字母字符
  • 只有一张桌子device_types,看起来还可以
【解决方案2】:

将您标记为无符号的 string 数据类型更改为 integer 数据类型

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 2019-08-08
    • 2021-09-15
    • 1970-01-01
    相关资源
    最近更新 更多