I would like to know if there is any possibility to add string referenced foreign key in laravel migration. So, I have this codes on first file migration.
public function up()
{
// roles table
Schema::create('roles', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('level')->unsigned();
$table->string('title');
$table->unique('level');
});
// account table
Schema::create('account', function(Blueprint $table)
{
$table->increments('id');
$table->integer('acc_no');
$table->string('acc_username');
$table->string('acc_password');
$table->integer('acc_roles_level')->unsigned();
$table->softDeletes();
$table->timestamps();
$table->unique('acc_username');
});
then on the second migration file I have this code:
public function up()
{
Schema::table('account', function(Blueprint $table)
{
$table->foreign('acc_roles_level')
->references('level')->on('roles')
->onUpdate('cascade')
->onDelete('set null');
});
}
when migration run, it shows an error:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `account` add constraint account_acc_roles_level_foreign foreign key (`acc_roles_level`) references `roles` (`level`) on delete set null on update cascade) on update cascade)
I dont know why I can't make references to string column. meanwhile, I successfully run this code without error on mysql:
alter table account add foreign key acc_roles_level references roles(level)
is there anyone has encountered the similar issues?