I'm working with Laravel 5.8 to develop my project, and I just create a Migration goes like this:
public function up()
{
Schema::create('user_wallet_transactions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedSmallInteger('user_id');
$table->foreign('user_id')->references('usr_id')->on('users');
$table->unsignedSmallInteger('wallet_id');
$table->foreign('wallet_id')->references('id')->on('wallets');
$table->string('amount');
$table->string('description');
$table->timestamps();
});
}
But when I run this I get this error:
SQLSTATE[HY000]: General error: 1005 Can't create table `nanonew_main`.`user_wallet_transactions` (errno: 150 "Foreign key
constraint is incorrectly formed") (SQL: alter table `user_wallet_transactions` add constraint `user_wallet_transactions_user_id_foreign` foreign key (`user_id`)
references `users` (`usr_id`))
I don't know what is going wrong here! My table users
goes like this and have the field named usr_id
:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('usr_id');
$table->string('usr_name')->unique();
$table->boolean('usr_is_admin')->default(0)->comment('0 = admin , 1 = not admin');
$table->boolean('usr_is_active')->default(0);
$table->string('usr_email')->unique()->nullable();
$table->timestamp('usr_email_verified_at')->nullable();
$table->string('usr_password');
$table->rememberToken();
$table->timestamps();
$table->engine = 'InnoDB';
});
}
And table wallets
also have the field named id
:
public function up()
{
Schema::create('wallets', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->string('name')->unique();
$table->tinyInteger('is_active');
$table->tinyInteger('is_cashable');
$table->timestamps();
});
}
So what's the issue here? How can I fix it?
I would really appreciate any idea or suggestion from you guys...
Thanks in advance.