N
Neon3mo ago
sensitive-blue

Laravel Migration to Neon Error

When I try to migrate my laravel database to Neon's server, I keep getting this error:
SQLSTATE[25P02]: In failed sql transaction: 7 ERROR: current transaction is aborted, commands ignored until end of transaction block (Connection: pgsql, SQL: alter table "provinces" add constraint "provinces_country_id_foreign" foreign key ("country_id") references "countries" ("id") on delete cascade)
SQLSTATE[25P02]: In failed sql transaction: 7 ERROR: current transaction is aborted, commands ignored until end of transaction block (Connection: pgsql, SQL: alter table "provinces" add constraint "provinces_country_id_foreign" foreign key ("country_id") references "countries" ("id") on delete cascade)
for context: I have 2 tables; countries and provinces, to which provinces references countries via country_id like so:
Schema::create('countries', function (Blueprint $table) {
$table->id();
});
Schema::create('countries', function (Blueprint $table) {
$table->id();
});
Schema::create('provinces', function (Blueprint $table) {
$table->id();

// $table->unsignedBigInteger('country_id');

$table->foreignId('country_id')->default(1)->constrained('countries')->cascadeOnDelete();

// $table->foreign('country_id')->references('id')->on('countries')->cascadeOnDelete();

// $table->foreignId('country_id')->references('id')->on('countries');
});
Schema::create('provinces', function (Blueprint $table) {
$table->id();

// $table->unsignedBigInteger('country_id');

$table->foreignId('country_id')->default(1)->constrained('countries')->cascadeOnDelete();

// $table->foreign('country_id')->references('id')->on('countries')->cascadeOnDelete();

// $table->foreignId('country_id')->references('id')->on('countries');
});
The commented code are the attempts at restructuring the foreignId in hopes that it would work. But the thing is, there are other files in my migrations that have foreignId's set like the ones above and they're working fine, here's an example:
Schema::create('users', function (Blueprint $table) {
$table->id();

$table->foreignId('user_role_id')->default(1)->constraint('user_roles')->cascadeOnDelete();
$table->foreignId('user_status_id')->default(1)->constraint('user_statuses')->cascadeOnDelete();
});
Schema::create('users', function (Blueprint $table) {
$table->id();

$table->foreignId('user_role_id')->default(1)->constraint('user_roles')->cascadeOnDelete();
$table->foreignId('user_status_id')->default(1)->constraint('user_statuses')->cascadeOnDelete();
});
Schema::create('user_roles', function (Blueprint $table) {
$table->id();
Schema::create('user_roles', function (Blueprint $table) {
$table->id();
Schema::create('user_permissions', function (Blueprint $table) {
$table->id();
Schema::create('user_permissions', function (Blueprint $table) {
$table->id();
All that I'm doing for this error to appear is using the
php artisan migrate:fresh
php artisan migrate:fresh
command in my terminal, or migrating the specific file individually. yes, the countries migration is running before the provinces. Any answer is appreciated!
2 Replies
deep-jade
deep-jade3mo ago
That's very interesting. Was there no other error message available?
sensitive-blue
sensitive-blueOP3mo ago
that's all I got from the terminal, here's the whole error message:
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "countries" does not exist (Connection: pgsql, SQL: alter table "provinces" add constraint "provinces_country_id_foreign" foreign key ("country_id") references "countries" ("id") on delete cascade)

at vendor\laravel\framework\src\Illuminate\Database\Connection.php:825
821▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
822▕ );
823▕ }
824▕
➜ 825▕ throw new QueryException(
826▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
827▕ );
828▕ }
829▕ }

1 vendor\laravel\framework\src\Illuminate\Database\Connection.php:571
PDOException::("SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "countries" does not exist")

2 vendor\laravel\framework\src\Illuminate\Database\Connection.php:571
PDOStatement::execute()
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "countries" does not exist (Connection: pgsql, SQL: alter table "provinces" add constraint "provinces_country_id_foreign" foreign key ("country_id") references "countries" ("id") on delete cascade)

at vendor\laravel\framework\src\Illuminate\Database\Connection.php:825
821▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
822▕ );
823▕ }
824▕
➜ 825▕ throw new QueryException(
826▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
827▕ );
828▕ }
829▕ }

1 vendor\laravel\framework\src\Illuminate\Database\Connection.php:571
PDOException::("SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "countries" does not exist")

2 vendor\laravel\framework\src\Illuminate\Database\Connection.php:571
PDOStatement::execute()
Update: I fixed the issue by including the
public $withinTransaction = false;
public $withinTransaction = false;
on all of my migration files

Did you find this page helpful?