laravelista / comments

Native comments for your Laravel application.
MIT License
745 stars 144 forks source link

Migration error #133

Closed Spot-up closed 3 years ago

Spot-up commented 3 years ago
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `comments` add index `comments_commenter_id_commenter_type_index`(`commenter_id`, `commenter_type`))

PHP 7.4.9, MySQL 5.7.31, Laravel 8.7.1, DB collate utf8_general_ci

databse.php

'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],

AppServiceProvider.php

use Illuminate\Support\Facades\Schema;
    public function boot()
    {
        Schema::defaultStringLength(191);
    }
mabasic commented 3 years ago

I haven't tested with a mysql database, I usually use sqlite. I will have to recreate the environment and apply a fix.

If you want to get this working for you asap you can publish the migrations (see readme) and set the key manually.

c_ci_ct_i

I think that that should work.

Spot-up commented 3 years ago
            $table->index(["commentable_type", "commentable_id"]);

and comment on this too?

mabasic commented 3 years ago

Change it to:

$table->index(["commentable_type", "commentable_id"], "ct_ci");

I think that should do it.

See laravel documentation for database migrations and setting the index name manually.

Spot-up commented 3 years ago

I added

        Schema::create('comments', function (Blueprint $table) {
            $table->bigIncrements('id');
             ...
            $table->timestamps();
            $table->engine = 'InnoDB';
        });

The migration was successful

mabasic commented 3 years ago

Shouldn't the engine by default be InnoDB?

Did you change the index name?

Send me the complete file that you have changed.

Spot-up commented 3 years ago

All my tables have MyISAM. I have not changed the Indices

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->bigIncrements('id');

            $table->string('commenter_id')->nullable();
            $table->string('commenter_type')->nullable();
            $table->index(["commenter_id", "commenter_type"]);

            $table->string('guest_name')->nullable();
            $table->string('guest_email')->nullable();

            $table->string("commentable_type");
            $table->string("commentable_id");
            $table->index(["commentable_type", "commentable_id"]);

            $table->text('comment');

            $table->boolean('approved')->default(true);

            $table->unsignedBigInteger('child_id')->nullable();
            $table->foreign('child_id')->references('id')->on('comments')->onDelete('cascade');

            $table->softDeletes();
            $table->timestamps();
            $table->engine = 'InnoDB';
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('comments');
    }
}

For some reason the migration date 2018_06_30_113500_create_comments_table.php

Spot-up commented 3 years ago

How to allow guests to reply to a comment

mabasic commented 3 years ago

How to allow guests to reply to a comment

Currently, guests can only submit new comments, not reply to existing. You could probably create your own logic for that functionality. Discussion on this topic should go to a new issue if needed.

Spot-up commented 3 years ago

OK, thanks