kitloong / laravel-migrations-generator

Laravel Migrations Generator: Automatically generate your migrations from an existing database schema.
MIT License
2.43k stars 269 forks source link

Error when reading Foreign Keys with SQLite #210

Closed TiagoSilvaPereira closed 4 months ago

TiagoSilvaPereira commented 5 months ago

Describe the bug I created a new Laravel 11 project with Jetstream + Teams, migrated the database to sqlite, then tried to run the package on this to recreate the migrations. By tying that, it fires the following exception:

Cannot assign null to property KitLoong\MigrationsGenerator\Database\Models\DatabaseForeignKey::$name of type string

Stack:

#0 ...\vendor\kitloong\laravel-migrations-generator\src\Database\SQLiteSchema.php(60): KitLoong\MigrationsGenerator\Database\Models\DatabaseForeignKey->__construct('team_invitation...', Array)
#1 [internal function]: KitLoong\MigrationsGenerator\Database\SQLiteSchema::KitLoong\MigrationsGenerator\Database\{closure}(Array, 0)

Terminal printscreen:

image

To Reproduce Steps to reproduce the behavior:

  1. Create a new Laravel project with Jetstream + Teams with sqlite database
  2. Migrate the database
  3. Run php artisan migrate:generate ...

Expected behavior The expected behavior is to generate the migrations without firing this exception

Details (please complete the following information):

Seems the problem happens in the following line of KitLoong\MigrationsGenerator\Database\Models\DatabaseForeignKey:

image

This is the data passed to the class constructor:

{
    "name": null,
    "columns": [
        "user_id"
    ],
    "foreign_schema": null,
    "foreign_table": "users",
    "foreign_columns": [
        "id"
    ],
    "on_update": "no action",
    "on_delete": "no action"
}
fernandobugni commented 4 months ago

Sorry, I have the same issue. Is there any solution or tip to avoid it? Thanks!

fernandobugni commented 4 months ago

Hi, me again I solve it coding the following in DatabaseForeignKey.php:38 $this->name = $foreignKey['name'] ?? $table . '_fk'; Please, anyone who could let comment if it is correct or not

TiagoSilvaPereira commented 4 months ago

Hi, me again I solve it coding the following in DatabaseForeignKey.php:38 $this->name = $foreignKey['name'] ?? $table . '_fk'; Please, anyone who could let comment if it is correct or not

I think this solution will not work correctly because it will produce the same index name for all foreign keys.

fernandobugni commented 4 months ago

Yes, I forgot not to use the same names, just in case to have an approach of a possible solution

kitloong commented 4 months ago

Sorry for late and thanks for the details.

The SQLite foreign key name is null, I have fixed this issue in v7.0.2

TiagoSilvaPereira commented 4 months ago

Sorry for late and thanks for the details.

The SQLite foreign key name is null, I have fixed this issue in v7.0.2

Thank you very much, @kitloong. You rock!! Also, could you please add a monthly sponsorship option to support this project? It seems buy me a coffee is not working in my country

kitloong commented 4 months ago

Hi @TiagoSilvaPereira , huge thank you for your kind words and support.

I'm excited to share that I have added a sponsor option through Github Sponsor at https://github.com/sponsors/kitloong.

There is absolutely no pressure to do so, your support in any form, be it through contributions, feedback, or even spreading the word, is invaluable.

TiagoSilvaPereira commented 4 months ago

@kitloong Thank you. Github Sponsors worked well. I'm using your package inside Vemto 2, an application for Laravel developers currently in Alpha. May I also offer you a Lifetime license as a thank you? Vemto currently has a lot of bugs, haha, but I'm sure you will find it useful

kitloong commented 4 months ago

Thank you so much for your kind offer.

I'm glad to hear that my package could be part of Vemto 2. I'd be delighted to accept the license and am excited to explore how Vemto evolves.

Bugs are just part of the journey in software development 😎

I appreciate your generosity.

TiagoSilvaPereira commented 4 months ago

Awesome @kitloong , I'll send you an email from contact@vemto.app. Best regards!