cakephp / cakephp/phinx

Can't use a built-in SQL function as a default value with SQL Server

Open
#1,100 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
PHP
Stars
4.5k
Forks
884
PR merge metrics
No merged PRs in 30d

Description

I have a column 'created' and 'modified' which I would like to default to SQL Server's getutcdate() function. However, setting this as the default in Phinx results in a default value of a literal string 'getutcdate()', which causes SQL Errors when creating the row and 'created' or 'modified' are not specified.

The same occurs with a datatype of uuid which I set the default to newid() in SQL Server. Phinx converts this to a string 'newid()', resulting in SQL errors if the column isn't specified.

For example:

$table = $this->table('objects');
$table->addColumn('created', 'datetime', ['default' => 'getutcdate()', 'null' => false]);
$table->create()

Generates this SQL:

CREATE TABLE objects (
  id INT NOT NULL IDENTITY(1,1),
  created DATETIME NOT NULL DEFAULT 'getutcdate()',  # Notice the quotes
CONSTRAINT PK_objects  PRIMARY KEY ([id]));

When I need it to generate this SQL:

CREATE TABLE objects (
  id INT NOT NULL IDENTITY(1,1),
  created DATETIME NOT NULL DEFAULT (getutcdate()),  # No quotes!
CONSTRAINT PK_objects  PRIMARY KEY ([id]));

I haven't tried this with other SQL adapters, so I don't know if this applies to them as well.

Presently the work around is to manually define the default constraint via $this->execute().

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

The report does not name files or tests; start by reproducing the migration with the SQL Server adapter and trace how the column default is rendered. Done means function defaults such as getutcdate() and newid() are emitted without string quotes while ordinary defaults remain valid.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.