cakephp / cakephp/phinx

Set default value of a column to unhex(replace(uuid(),'-',''))

Open
#2,245 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

support waiting on feedback
Dominant language
PHP
Stars
4.5k
Forks
884
PR merge metrics
No merged PRs in 30d

Description

Hi guys,

We're in the process of migrating our database migrations into phinx.

Our tables all use binary(16) as datatype for the Primary Key column.
To generate that value we set the default for the primary key to unhex(replace(uuid(),'-','')).

How would I be able to set that particular default using Phinx?

I tried the following (migration generated by https://github.com/odan/phinx-migrations-generator):

 `$this->table('Roles',` [
        'id' => false,
        'primary_key' => ['RoleID'],
        'engine' => 'InnoDB',
        'encoding' => 'utf8mb4',
        'collation' => 'utf8mb4_general_ci',
        'comment' => 'nolink=MenuItems_Roles',
        'row_format' => 'DYNAMIC',
    ])
        ->addColumn('RoleID', 'binary', [
            'null' => false,
            'default' => 'unhex(replace(uuid(),\'-\',\'\'))',
            'limit' => 16,
        ])
        ->addColumn('Name', 'string', [
            'null' => false,
            'limit' => 45,
            'collation' => 'utf8mb4_general_ci',
            'encoding' => 'utf8mb4',
            'after' => 'RoleID',
        ])
        ->addColumn('MenuItemID', 'binary', [
            'null' => true,
            'default' => null,
            'limit' => 16,
            'comment' => 'render=translate',
            'after' => 'Name',
        ])
        ->addColumn('LastModified', 'timestamp', [
            'null' => false,
            'default' => 'current_timestamp()',
            'after' => 'MenuItemID',
        ])
        ->addColumn('LastModifiedByID', 'binary', [
            'null' => true,
            'default' => null,
            'limit' => 16,
            'after' => 'LastModified',
        ])
        ->addIndex(['Name'], [
            'name' => 'Name',
            'unique' => true,
        ])
        `->create();`

This results in:

'PDOException: SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'RoleID' in /vendor/robmorgan/phinx/src/Phinx/Db/Adapter/PdoAdapter.php:198'

Also tried 'default' => 'unhex(replace(uuid(),"-",""))',. Same error.

Any suggestions?

Thanks!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the migration shown in the issue and the error path at src/Phinx/Db/Adapter/PdoAdapter.php:198. Trace how column defaults are rendered for the database adapter, then check whether an expression such as unhex(replace(uuid(),'-','')) can be represented without being quoted as a literal. Done means the migration creates the binary(16) column with the requested default and no invalid-default error.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
databases
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.