bpuig / laravel-subby

Laravel Plan and Subscriptions manager.
https://bpuig.github.io/laravel-subby
MIT License
104 stars 42 forks source link

Default database migration isn't compatible with MySQL #122

Closed hlorofos closed 2 years ago

hlorofos commented 2 years ago

The following migration with default table settings producing MySQL error:

  SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'plan_subscription_schedules_scheduleable_type_scheduleable_id_index' is too long (SQL: alter table `plan_subscription_schedules` add index `plan_subscription_schedules_scheduleable_type_scheduleable_id_index`(`scheduleable_type`, `scheduleable_id`))
        Schema::create(config('subby.tables.plan_subscription_schedules'), function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('subscription_id');
            $table->morphs('scheduleable');
            $table->timestamp('scheduled_at')->nullable();
            $table->timestamp('failed_at')->nullable();
            $table->timestamp('succeeded_at')->nullable();

            $table->unique(['subscription_id', 'scheduleable_type', 'scheduleable_id', 'scheduled_at'], 'unique_plan_subscription_keys');

            $table->foreign('subscription_id', 'plan_subscription_fk')->references('id')->on(config('subby.tables.plan_subscriptions'))->onDelete('cascade')->onUpdate('cascade');
        });

Workaround is to edit default table name in config/shubby.php to a shorter one.

Rndwiga commented 2 years ago

Thanks for the post. I got stuck on the same issue. Changing "plan_subscription_schedules" to "subscription_schedules" resolved the issue. This was on MariaDB 10.6.7

bpuig commented 2 years ago

Hello, It has a name to make it shorter "unique_plan_subscription_keys", but maybe mariaDB does not support it.

bpuig commented 2 years ago

Found that the bug was not the unique index but in $table->morphs('scheduleable');. v7 fixes many migration errors :+1:

aeq-dev commented 2 years ago

Hi @bpuig Still the same issue here after updating to v7.0 I fixed it by renaming my tables in config as bellow :

'tables' => [
        'plans' => 'plans',
        'plan_combinations' => 'p_combinations',
        'plan_features' => 'p_features',
        'plan_subscriptions' => 'p_subscriptions',
        'plan_subscription_features' => 'p_subscription_features',
        'plan_subscription_schedules' => 'p_subscription_schedules',
        'plan_subscription_usage' => 'p_subscription_usage',
    ],
bpuig commented 2 years ago

Then maybe you should check this: https://laravel.com/docs/9.x/migrations#index-lengths-mysql-mariadb If that fixes your issue maybe I can add it to the docs.

aeq-dev commented 2 years ago

@bpuig Yes that's the problem Just add Schema::defaultStringLength(191);

Thanks