Reputation: 1342
I have a spelling error in migrations:
Schema::create('business_category',function(Blueprint $table){
$table->integer('business_id')->unsinged();
$table->integer('category_id')->unsinged();
});
Schema::create('business_category',function(Blueprint $table){
$table->foreign('business_id')->references('id')->on('business');
$table->foreign('category_id')->references('id')->on('category');
});
and I run "php artisan migrate" this error has been shown:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'brandecide.#sql-42 4_aa' (errno: 150) (SQL: alter tablebusiness_category
add constraint bus
iness_category_business_id_foreign foreign key (business_id
) referencesbusiness
(id
))
this error caused by:
$table->integer('business_id')->**unsinged**();
and I should change this to
$table->integer('business_id')->**unsigned**();
to fix it. How should I understand this from the error?
Upvotes: 0
Views: 94
Reputation: 152880
How should I understand this from the error?
You can't, unfortunately. I agree the error is quite misleading. The problem is that unique()
is not a real method. (If so you'd get a method undefined exception)
Instead the call to unique()
ends up being caught by Illuminate\Support\Fluent
public function __call($method, $parameters)
{
$this->attributes[$method] = count($parameters) > 0 ? $parameters[0] : true;
return $this;
}
And is then added to $this->attributes
without doing any checking. This then results in no error but just an attribute (unsinged
) that will never be used and a missing unsigned
that causes the constraint to fail.
If you want you can create an issue (with type "proposal") on github. Maybe someone has a good idea how this can be prevented. (e.g. a whitelist of recognized methods)
Upvotes: 2
Reputation: 6176
You probably already have some entries in your business_category
table. When running that migration, you're trying to add a column that references another table. As you didn't define any default value, MySQL tries to default the value to null
, but that corresponds to nothing in that other table. When having a foreign key, the value in that column MUST correspond to a value in another table.
So to solve it, you could create a ->default(1)
, assuming that the id 1
exists in your business
-table.
Upvotes: 0