1
votes

I've created entities on my Symfony project, but when I execute migrations, I received this error:

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

I've created these two entities 'Post' and 'Tag' with ManyToMany associations:

App\Entity\Post

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Tag", inversedBy="posts")
 * @ORM\JoinTable(name="post_tag",
 *      joinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="tag_code", referencedColumnName="code")}
 *      )
 */
private $tags;

App\Entity\Tag:

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Post", mappedBy="tags")
 */
private $posts;

The migrations should create the pivot table 'post_tag' with post_id and tag_code as primary keys. How can I fix the error?

3
Is your charset for doctrine in config.yml set to UTF8? doctrine: dbal: charset: UTF8 - J.Hpour
@J.Hpour no, it sets to: utf8mb4 - Mintendo
Can you change it to utf8 and try to execute migration again? - J.Hpour

3 Answers

0
votes

let you try adding the following code in to boot() method of AppServiceProvider.php:

Schema::defaultStringLength(191);
0
votes

Add to AppServiceProviders.php:

use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}
-1
votes

I resolved the problem changed the length of key from my 'post_tag' table