i want to make a one two many relationship between two tables medicines and suppliers, so i made a foreign id of the table suppliers in medicines table.
public function up(): void
{
Schema::create('medicines', function (Blueprint $table) {
$table->id();
$table->foreignId('supplier_id')->constrained('suppliers');
$table->string('name');
$table->string('price');
$table->string('what_is_used_for');
$table->string('age_group');
$table->string('quantity');
$table->softDeletes();
$table->timestamps();
$table->date('expiration_date')->nullable();
});
}
public function up(): void
{
Schema::create('suppliers', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email');
$table->string('contact_number');
$table->string('address');
$table->timestamps();
$table->softDeletes();
});
}
but this is the error i’m getting:
SQLSTATE[HY000]: General error: 1005 Can’t create table mypharmacy
.medicines
(errno: 150 “Foreign key constraint is incorrectly formed”) (Connection: mysql, SQL: alter table medicines
add constraint medicines_supplier_id_foreign
foreign key (supplier_id
) references suppliers
(id
))
meryem arika is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.