1 year 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 1 year 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->string('device_model', 32)->primary();
            $table->string('description', 100);

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

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.