1

Every time I create a new Entity with attributes as the type string, whenever I wish to update my database I receive the error:

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

Now this is something many people have faced and reasons are described here. I understand what my problem is and I can solve it by giving the property @ORM\Column(type="string", length=191).

I have to set the length to 191 for every single string type in my schema though. Is there any way though I can set the default length for a string to 191 in a (doctrine) config file so I don't have to change this every single time?

I do not wish to change to utf8.

Dirk J. Faber
  • 4,360
  • 5
  • 20
  • 58
  • Possible duplicate of [How to fix MySql: index column size too large (Laravel migrate)](https://stackoverflow.com/questions/42043205/how-to-fix-mysql-index-column-size-too-large-laravel-migrate) – dbrumann Feb 23 '19 at 15:27
  • You can change the configuration for MySQL to increase the available size: https://stackoverflow.com/questions/42043205/how-to-fix-mysql-index-column-size-too-large-laravel-migrate/52778785#52778785 – dbrumann Feb 23 '19 at 15:28
  • Did you solve it? – miguelbemartin Aug 15 '19 at 14:13
  • 1
    @miguelbemartin, yes. Updating my database server (MariaDB) to version 10.3.15 solved my problem. – Dirk J. Faber Aug 15 '19 at 18:02

2 Answers2

0

For Symfony 6+ code below may help. Just set length for column with migration name in doctrine_migrations.yaml:

doctrine_migrations:
  storage:
    table_storage:
        table_name: 'migration_version'
        version_column_name: 'version'
        version_column_length: 255
        executed_at_column_name: 'executed_at'
AGriboed
  • 608
  • 4
  • 5
0

In Symfony 2.7+ you can register an EventListener for Doctrine's loadClassMetadata event, this way:

<?php

namespace App\Doctrine;

use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;

#[AsDoctrineListener('loadClassMetadata')]
readonly class StringLengthListener
{
    public function __construct(private int $defaultStringLength = 191)
    {
    }

    public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
    {
        $classMetadata = $eventArgs->getClassMetadata();

        foreach ($classMetadata->fieldMappings as &$fieldMapping) {
            if ('string' === $fieldMapping['type'] && (!isset($fieldMapping['length']) || $fieldMapping['length'] > $this->defaultStringLength)) {
                $fieldMapping['length'] = $this->defaultStringLength;
            }
        }
    }
}

The "AsDoctrineListener" attribute will register the event listener and in the loadClassMetadata method, you'll set/overwrite the length of all defined string columns, with no length set. If the length is greater than the given $defaultStringLength, the previous length is overwritten.

In case you use a lower Symfony version, you can register the event listener the old fashioned way, using services.yaml. All variants are well documented here: https://symfony.com/doc/current/doctrine/events.html

Armin
  • 15,582
  • 10
  • 47
  • 64