Here's the problem: I'm trying to generate a very very simple custom primary key
(by overriding function getKey()
) for my table Users
. When I migrate the table, it says:
SQLSTATE[42000]: Syntax Error or Acccess Violation: 1072 Key Column 'uid' Doesn't Exist in Table (SQL: Alter Table 'users' Add Primary Key 'users_uid_primary'('uid'))
Thanks in advance for your suggestions and corrections.
EDIT: After migration, my table is created in MySQL but with no primary key column (in this case 'uid').
Schema:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->primary('uid');
$table->string('username');
$table->string('password');
$table->timestamps();
});
}
Model:
class User extends Model
{
protected $primaryKey = 'uid'; //emphasize that the primary key isn't 'id'
protected $keyType = 'string'; //p.k. holds string values
public $incrementing = false; //then p.k. must not auto increment it
public $timestamps = false; //no need for this one
public function getKey() {
return "u".rand(1000, 9999)."r"; //return something like 'u2548r'
}
}
You have to actually create the field ... you are setting a non existing field uid
as the primary key.
You need to define the field as some type and add it the table. Blueprint@primary
is setting an index (in general terms) not creating a field.