Error when migrating with Laravel in Heroku to Postgres - laravel

I'm working with Laravel 5.6 and Heroku with Postgres database.
I have uploaded my application to Heroku, the login form shows it to me correctly.
The problem comes when doing the migration of the tables with:
heroku run php artisan migrate and using Postgres of Heroku, I made all the configuration correctly but when doing migrate this happens:
In Connection.php line 664:
SQLSTATE[42830]: Invalid foreign key: 7 ERROR: there is no unique
constraint matching given keys for referenced ta ble "users" (SQL:
alter table "sales" add constraint "sales_user_id_foreign" foreign key
("user_id") references "us ers" ("id"))
In Connection.php line 458:
SQLSTATE[42830]: Invalid foreign key: 7 ERROR: there is no unique
constraint matching given keys for referenced ta ble "users"
The strange thing is that the same migration locally does not cause me any problems (MySQL) but Heroku with Postgres show the error.
These are my User and Sales migrations:
Users:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->integer('id')->unsigned();
$table->foreign('id')->references('id')->on('people')->onDelete('cascade');
$table->string('user')->unique();
$table->string('password');
$table->boolean('state')->default(1);
$table->integer('idRole')->unsigned();
$table->foreign('idRole')->references('id')->on('roles');
$table->rememberToken();
//$table->timestamps();
});
}
Sales:
public function up()
{
Schema::create('sales', function (Blueprint $table) {
$table->increments('id');
$table->integer('client_id')->unsigned();
$table->foreign('client_id')->references('id')->on('people');
$table->unsignedInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->dateTime('date_time');
$table->decimal('total', 11, 2);
$table->string('status', 20);
$table->timestamps();
});
}
I do not know why I have this error in Heroku but everything works locally. How can I fix this?

It's because the id column on the table users does not have the unique constraint.
You can set the id column as auto-increment and run the migration again:
Users
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
// Other codes
});
}

The strange thing is that the same migration locally does not cause me any problems (MySQL) but Heroku with Postgres show the error.
It's always a good idea to use the same database software in development and production. MySQL and PostgreSQL aren't drop-in replacements for each other, even with an ORM and a schema migration tool. There are definitely situations where one will be happy and the other won't.
I suspect that if you use MySQL on Heroku your issue will disappear. Heroku doesn't provide MySQL itself, but there are a couple of addons that you could use. JawsDB and ClearDB are both popular options, or if you prefer you could host your own MySQL database on something like RDS.
You could also switch to Postgres locally and see if you can reproduce the error.

Related

How do i get my cascadeOnDelete to work in laravel

Trying to learn Laravel, i am following a tutorial from laracast called Laravel 8 from scratch and has now reached episiode about cascades
https://laracasts.com/series/laravel-8-from-scratch/episodes/53
I'm not getting any errors but if i add a comment to a post, and then delete the post then my comment stays in the database, while his is removed with the post. I tried deleting the post using laravel, but also using tableplus.
My setup is a bit diffrent from his so i'm wondering if my cascades arent working because of something in my setup.
comment migration
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->foreignId('post_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->text('body');
$table->timestamps();
});
Post migration
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id');
$table->foreignId('category_id');
$table->string('slug')->unique();
$table->string('title');
$table->text('excerpt');
$table->text('body');
$table->timestamps();
$table->timestamp('published_at')->nullable();
});
I've also tried using $table->foreignKey('post_id')->references('id')->on('posts')->onDelete('cascade) but i cant seem to get it working.
I have also tried adding the constrain my self at the mySql database using
alter table
`comments`
add
constraint `comments_post_id_foreign` foreign key (`post_id`) references `posts` (`id`) on
delete cascade
Do i need a specific use, or could it be my setup?
php/database setup versions from wamp
I think if you search for myisam and innodb, you will probably find the answer.
in myisam, foreign keys are not deleted

Stripe cashier, ignore customers migration

I installed Laravel Cashier, I migrated the tables but when I want to re-migrate after the first one with a fesh --seed it tells me that there is a problem with the customers table of cashier.
I published the vendors but I find myself with the customers & users table, I deleted the customers table but it still keeps it for me, so I ignored the migrations and kept the 2 subscription tables of cashier but it still doesn't work:
Cannot declare class CreateSubscriptionsTable, because the name is already in use
at database/migrations/2021_07_04_000002_create_subscriptions_table.php:7
Cannot declare class CreateSubscriptionsTable, because the name is already in use
I added this in AppServiceProvider :
Cashier::ignoreMigrations();
Here is my user table, I added the fields of the customers table:
Schema::create('users', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->foreignUuid('rank_id')->constrained('ranks');
$table->string('stripe_id')->nullable();
$table->string('pm_type')->nullable();
$table->string('pm_last_four', 4)->nullable();
$table->timestamp('trial_ends_at')->nullable();
$table->string('username');
$table->string('email');
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->integer('profile_visibility')->default(\App\Models\User::PROFILE_PUBLIC);
$table->integer('status')->default(0);
$table->timestamp('last_seen_feed')->default(Carbon::now()->toDateTimeString());
$table->timestamp('last_seen_comments')->default(Carbon::now()->toDateTimeString());
$table->rememberToken();
$table->timestamps();
});
php artisan migrate:fresh might be the solution here, try add seeders from different command php artisan db:seed --class:NameofSeedSeeder
More under documentation: https://laravel.com/docs/8.x/migrations

Laravel - How to modify column with migration?

I added foreign key to my table, but forgot to make it nullable().
How can I change column now? As I understand I have to create new migration file with --table flag and add something like: ->nullable()->change();
Correct?
Yes, you need to create another migration or you rollback your previous migration.
There is an example on the Laravel page for modifying columns:
Schema::table('users', function (Blueprint $table) {
$table->string('name', 50)->nullable()->change();
});
See also this link
https://laravel.com/docs/5.8/migrations#modifying-columns
If you prefer a rollback see this link.
https://laravel.com/docs/5.8/migrations#rolling-back-migrations

How to transform an existing column in foreign key using Laravel Migrations

I'm having trouble trying to change a column type in laravel to fits it as a compatible column to be a foreign key referencing another table id fields.
I have a a schema like this:
Schema::create('person_organization', function(Blueprint $table){
...
$table->integer('organization_id');
...
});
and I want to change the field organization_id to an unsigned type, which will make it able to be a foreign key referencing the id field in the organizations table.
NOTE: Just changing the field type in the creation of the table is not an available option, because the system is running in production mode.
So we need to make a new migration to do these changes.
NOTE 2: i tried the method change as described in laravel docs, but it sticks in a query error, as following:
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') on delete cascade' at line 1 (SQL: alter table person_organization add constraint person_organization_person_id_foreign foreign key (person_id) references persons () on delete cascade)
Considering you have already installed doctrine/dbal package in your application
Now create migration php artisan make:migration your_migration_name and then in migration insert the below code.
Schema::table('persons', function(Blueprint $table) {
$table->integer('organization_id')->unsigned()->index()->change();
$table->foreign('organization_id')->references('id')->on('organizations')-
>onDelete('cascade');
})
now run command php artisan migrate and now your are done.
Happy coding...
From Laravel 5.6 docs:
Before modifying a column, be sure to add the doctrine/dbal dependency to your composer.json file.
composer require doctrine/dbal
Then create the migration:
php artisan make:migration add_organization_foreign_to_persons_table --table=persons
And:
Schema::table('persons', function (Blueprint $table) {
$table->unsignedInteger('organization_id')->change();
$table->foreign('organization_id')->references('id')->on('organizations');
});
$table->integer('organization_id')->unsigned();
$table->foreign('organization_id')->references('id')->on('organizations')->onDelete('cascade');

Creation of table columns using artisan migration

Using artisan migrate command, i am trying to create the table. users table is created but not the columns. What could I check ?
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
$table->string('email');
$table->string('password');
$table->timestamps();
});
}
Upon running the above migration file, the table and its columns are successfully created in the database. If you see that the users table is created but not the columns in it, then you might not be looking properly.
I am writing the following answer on my assumption that you're very new to web development also and might not be acquainted with the tools yet.
I assume you're using phpMyAdmin to view your database. If you go to your database and then the users table, you'll be in the Browse tab. Of course, it will be empty because there are no data inserted in the users table, but if you click the Structure tab, then you can view the schema of your users table and see that the columns are created.

Resources