0

I set up a new project with symfony and doctrine. I created a database using the following command:

php bin/console doctrine:database:create

Then I created an entity using the following command:

php bin/console make:entity

This was the code generated:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
class User
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $email;

    /**
     * @ORM\Column(type="string", length=24, nullable=true)
     */
    private $phone;

    /**
     * @ORM\Column(type="integer")
     */
    private $zip_code;

    public function getId()
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): self
    {
        $this->email = $email;

        return $this;
    }

    public function getPhone(): ?string
    {
        return $this->phone;
    }

    public function setPhone(?string $phone): self
    {
        $this->phone = $phone;

        return $this;
    }

    public function getZipCode(): ?int
    {
        return $this->zip_code;
    }

    public function setZipCode(int $zip_code): self
    {
        $this->zip_code = $zip_code;

        return $this;
    }
}

When I try to run the migration, I keep getting the following error:

php bin/console make:migration

And

php bin/console doctrine:migrations:migrate

Error:

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

This is extremely frustrating as I am using the command line generate code and a brand new project and have been trying to troubleshoot this for quite some time.

I appreciate any suggestions on how to resolve.

AnchovyLegend
  • 12,139
  • 38
  • 147
  • 231

1 Answers1

0

This happened to me because I used utf8mb4 as collation (more specifically utf8mb4_unicode_ci), which is now default for doctrine.

https://github.com/symfony/recipes/blob/master/doctrine/doctrine-bundle/1.6/config/packages/doctrine.yaml#L13

The strange thing is that I only had this problem with unique fields. I had to limit those to 191 characters, an answer I found here: https://laravel-news.com/laravel-5-4-key-too-long-error

Padam87
  • 1,013
  • 1
  • 6
  • 7
  • Thanks for the reply. I saw the post you linked to. I changed all my lengths to 191 and ran the migration make and migration migration commands again, no luck. I also am using the exact sam collate that is specified in the doctrine.yaml file. – AnchovyLegend Jul 26 '18 at 13:18
  • Just for a test, could you change that to simple utf8, generate and run migrations again? – Padam87 Jul 26 '18 at 13:19
  • thats what I did initially, no luck. – AnchovyLegend Jul 26 '18 at 13:20
  • That is really weird, changing the collation to utf8_unicode_ci should have fixed it. I have no other ideas. – Padam87 Jul 26 '18 at 13:26
  • Could you post the generated migration? (I want to run it on my setup) – Padam87 Jul 26 '18 at 13:27
  • I removed all the migrations from src/Migrations, changed the charset to utf8 and ran make migration and the migrate command and it worked! – AnchovyLegend Jul 26 '18 at 13:31