Skip to content

Fixed length BINARY with index results in SQL error since v4.7.0 #999

@Phally

Description

@Phally

Since migrations version 4.7.0 when trying to create a BINARY(20) field with an index on it, MySQL will fail as the migrations will create the field as a TINYBLOB now instead of BINARY or VARBINARY. When using BLOB indexes require lengths to tell MySQL how much of the BLOB it should index. There is a way to do it in MySQL, but that can't be done with migrations either (requires a custom query). Edit: which can be done with $this->index('hash')->setLimit(), but that should be baked then probably to prevent the error.

I read there were some changes in 4.7.0 regarding this. Is this behavior intentional and if so, what is the best way to do this then?

Prior to 4.7.0 it worked as expected. Also bake will still create the migration properly as a binary type.

I did quickly try the builtin backend and specifying a custom type using TypeFactory::map(), but it didn't seem to pick that up. I still got the same result at least.

Migration code (baked from a BINARY(20) column):

$this->table('files')
    ->addColumn('hash', 'binary', [
        'after' => 'hash',
        'default' => null,
        'length' => 20,
        'null' => true,
    ])
    ->addIndex(
        $this->index('hash')
            ->setName('hash')
            ->setType('unique')
    )
    ->update();

Error:

SQLSTATE[42000]: Syntax error or access violation: 1170 BLOB/TEXT column 'hash' used in key specification without a key length
Query: ALTER TABLE `files` ADD  UNIQUE KEY `hash` (`hash`);

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions