Skip to content

default column spec expression misses parentheses #2652

@Kek5chen

Description

@Kek5chen

Description

In SQLite (and possibly other systems, untested) there's a bug where the DEFAULT column spec will not generate parentheses around SimpleExpr-s which are expressions and not literals. This is invalid.

A column like this:

<TableCreateStatement>
.col(integer_uniq(Numbers::Two).default(Expr::val(1).add(1)))

will generate:

    "two"     integer        NOT NULL UNIQUE DEFAULT 1 + 1,

This is invalid as in SQLite (and possibly other dbms) anything after the DEFAULT spec needs to be wrapped in parentheses if it's not one of these:

  • NULL
  • a string constant
  • a blob constant
  • a signed-number

An explicit DEFAULT clause may specify that the default value is NULL, a string constant, a blob constant, a signed-number, or any constant expression enclosed in parentheses
-> Ref 3.2: https://sqlite.org/lang_createtable.html

Steps to Reproduce

  1. Create a new TableCreateStatement and assign it a column with a default expression that is not a Keyword or a literal value, but for example an expression like 1 + 1.
  2. Observe that the expression after the default column spec is not wrapped in parens.

Expected Behavior

It should be wrapped in parens like:

    "two"     integer        NOT NULL UNIQUE DEFAULT ( 1 + 1 ),

(padding between parentheses and expr is optional)

Actual Behavior

Parentheses are not being written to the sql statement.

Reproduces How Often

Always.

Workarounds

Wrapping the Expr into a Expr::tuple([expr]) or an empty function ( like: Func::cust("").arg(expr)) will fix .

Extra

It also seems like the Expr::col in such contexts wraps the column name with "-quotes in sqlite. That's invalid. I don't have an exact spec for this on hand but this describes the issue: typeorm/typeorm#7963

Versions

v1.1.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions