【问题标题】:Syntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not n语法错误或访问冲突:1064:在 'unsigned not null、modelName varchar(191) not null、title varchar(191) not n 附近使用的语法
【发布时间】:2018-02-02 11:41:58
【问题描述】:

在我开始使用 users 表 ID 作为 blogs 表中的外键并尝试将其迁移到数据库之前,一切正常。我开始收到

的错误

SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册以获取正确的语法 ax 在第 1 行使用'unsigned not null, modelName varchar(191) not null, title varchar(191) not n' (SQL: create table blogs (id int unsigned not null auto_increment 主键,user_idvarchar(191) unsigned not null,modelNamevarchar(191) not null,titlevarchar(191) not null,priceint unsigned not null,description text not n ull, status int not null, photo_id varchar(191) not null, company_id varchar(191) not null default '1', created_at timestamp null, updated_at timestamp null) default c 字符集 utf8mb4 整理 utf8mb4_unicode_ci) 在 Connection.php 第 445 行: SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册以获取正确的语法 ax 在第 1 行的 'unsigned not null, modelName varchar(191) not null, title varchar(191) not n' 附近使用**

这是用户表结构

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('role_id')->index()->unsigned()->nullable();
            $table->integer('is_active')->default(0);
            $table->string('name');
            $table->string('photo_id')->default('default.png');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

这是博客表结构

 public function up()
    {
        Schema::create('blogs', function (Blueprint $table) {
            $table->increments('id');
            $table->string('user_id')->unsigned();
            $table->string('modelName')->index();
            $table->string('title');
            $table->integer('price')->unsigned();
            $table->text('description')->nullable(false);
            $table->integer('status');
            $table->string('photo_id');
            $table->string('company_id')->default(!null);
            $table->timestamps();

//            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('user_id')->refrences('id')->on('users');

        });
    }

据我所知,我几乎尝试了所有可能出错的方法,还尝试了在线可用的解决方案,但目前似乎没有任何效果。任何帮助将不胜感激。

【问题讨论】:

    标签: php database phpmyadmin migration laravel-5.4


    【解决方案1】:

    $table->string('user_id')->unsigned();

    string 类型的字段不能无符号。改为

    $table->integer('user_id')->unsigned();

    【讨论】:

    • 感谢您的更正 :) 当您连续编码八小时时会发生这种情况 :D
    • 但现在我收到 SQLSTATE[42000]: Syntax error or access violation: 1064 你的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的 ')' 附近使用正确的语法 (SQL: alter table blogs add constraint blogs_user_id_foreign foreign key (user_id) references users () ) 在 Connection.php 第 445 行: SQLSTATE[42000]: 语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的 ')' 附近使用正确的语法轴
    • 这是references,而不是refrences。睡一觉;)(也许接受这个答案)
    • 实际上我还有 2 个表 cmets 和 cmets_reply 分别使用 blog_id 和 comment_id 作为外国,我在那里检查了拼写,它们是正确的,所以我没有检查 f :Dor it there 因为我一直在复制粘贴。
    • 感谢@kerbholz 的帮助和建议 :) 现在我肯定要休息一下了 :D
    猜你喜欢
    • 1970-01-01
    • 2017-03-23
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 2018-09-12
    • 1970-01-01
    • 2015-09-12
    相关资源
    最近更新 更多