Shouldn't any foreign keys be in the same database?
Also see https://stackoverflow.com/questions/25952348/laravel-run-migrations-on-another-database
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi everyone. I have a Laravel project that uses two databases. In the main database, I store the tables that contain the list elements (e.g., statuses, types, categories). I use these list elements in forms. If I start a migration for a form in the main database and place the following line in the migration file, the migration runs without errors, and the table is added to the database (row: $table->foreignId('state_id')->constrained('list_states')->cascadeOnDelete();). However, if I reference a list element from the main database in the other database, the migration throws an error and doesn’t run.
Example list element migration code:
public function up(): void
{
Schema::create('list_states', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->boolean('is_visible')->default(false);
$table->softDeletes();
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('list_states');
}
Example property_list migration code (This table is added to the main database, where the list element table is also located.):
public function up(): void
{
Schema::create('property_lists', function (Blueprint $table) {
$table->id();
$table->longText('description');
$table->foreignId('state_id')->constrained('list_states')->cascadeOnDelete();
$table->foreignId('city_id')->constrained('list_cities')->cascadeOnDelete();
$table->text('seo_title')->nullable();
$table->text('seo_meta_description')->nullable();
$table->timestamps();
$table->softDeletes();
});
}
public function down(): void
{
Schema::dropIfExists('property_lists');
}
This table would be migrated to the second database, but it results in an error because the list_states table is not present or migrated in this database (of course, in the list_states model file, I specified that the table data should be fetched from the main database with protected $connection = 'mysql';).
public function up(): void
{
Schema::create('customers', function (Blueprint $table) {
$table->id();
$table->text('name')->nullable();
$table->integer('age')->nullable();
$table->text('bio')->nullable();
$table->foreignId('state_id')->constrained('list_states')->cascadeOnDelete();
$table->foreignId('city_id')->constrained('list_cities')->cascadeOnDelete();
$table->timestamps();
$table->softDeletes();
});
}
public function down(): void
{
Schema::dropIfExists('property_lists');
}
State Model code:
class State extends Model { use HasFactory;
protected $connection = 'mysql';
protected $table = 'list_states';
protected $fillable = ['name', 'slug', 'is_visible'];
public function cities()
{
return $this->hasMany(\App\Models\List\City::class, 'state_id');
}
}
Error msg:
General error: 1824 Failed to open the referenced table '$list_states' (Connection: tenant, SQL: alter table property_lists add constraint property_lists_state_id_foreign foreign key (state_id) references $list_states (id) on delete cascade)
Please or to participate in this conversation.