2
votes

Everything was working fine till I started using users tables id as a foreign key in blogs table and tried to migrate it into the database. I started getting errors of

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right synt ax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not n' at line 1 (SQL: create table blogs (id int unsigned not null auto_increment primary key, user_id varchar(191) unsigned not null, modelName varchar(191) not null, title varchar(191) not null, price int 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 haracter set utf8mb4 collate utf8mb4_unicode_ci) In Connection.php line 445: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right synt ax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not n' at line 1**

here is the users table structure

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();
        });
    }

and here's the blogs table structure

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

        });
    }

I have tried almost everything in my knowledge that could be going wrong and also tried solutions available online, but nothing seems to be working right now. Any help would be highly appreciated.

1

1 Answers

5
votes

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

Fields of type string cannot be unsigned. Change to

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