Set default value of a column to unhex(replace(uuid(),'-',''))
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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