AKmahato
7 months ago

SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined (SQL: alter table `device_models` add primary key `device_models_device_model_primary`(`device_model`))

Posted 7 months ago by AKmahato

I am trying to declare a varchar field as a primary key and that is a foreign key in a different table but when I migrating it's giving above error


This is my migration
--------------------------------

 public function up()
    {
        Schema::create('device_models', function (Blueprint $table) {
            $table->increments('id')->unsigned();
            $table->string('device_model', 32)->primary();
            $table->string('description', 100);
            $table->text('json');
            $table->timestamps();
        });
    }

And this is my child table
----------------------------------------
    
    public function up()
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->increment('id');
            $table->string('profile_name', 64);
            $table->string('device_model', 32)->nullable();
            // other fields
            $table->timestamps();
 
            $table->foreign('device_model')->references('device_model')->on('device_models');
        });
    }

This is my model
------------------------------
namespace App;

use Illuminate\Database\Eloquent\Model;

class DeviceModel extends Model
{
    protected $table = 'device_models';
 
    protected $primaryKey = 'device_model';
 
    public $incrementing = false;
    
    //public $keyType = 'string';
}


Please sign in or create an account to participate in this conversation.