question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default value is not escaped when using enum, which cause error: cannot use column reference in DEFAULT expression

See original GitHub issue

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

I have simple migration.

import {
  MigrationInterface,
  QueryRunner,
  Table,
} from 'typeorm';

export class CreateExampleable1578929385472 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.createTable(new Table({
      name: 'example',
      columns: [
        {
          name: 'id',
          type: 'int',
          isPrimary: true,
          isGenerated: true,
        },
        {
          name: 'enum',
          type: 'enum',
          enum: ['A', 'B', 'C'],
          default: 'A',
        },
      ],
    }));
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable('example');
  }
}

I am getting error: QueryFailedError: cannot use column reference in DEFAULT expression

SQL which cause issue: CREATE TABLE example(... "status" "example_enum_enum" NOT NULL DEFAULT A ... )

It should quote default value: CREATE TABLE example(... "enum" "example_enum_enum" NOT NULL DEFAULT 'A' ... )

I had to quote default value my self to make it work: (Without whitespaces, whitespaces added, to make it visible) default: " 'A' "

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:9

github_iconTop GitHub Comments

28reactions
petrusdemelocommented, Sep 7, 2020

The @agoulziprod 's solution don’t work to me. But that works:

{
   name: 'type',
   type: 'enum',
   enum: ['provider', 'client'],
   default: `'client'`,
}
11reactions
andrewdhazlettcommented, Jan 27, 2020

I had a similar error when adding a column with a default value, no db-level enum involved:

I was able to fix a similar issue (on postgres) by wrapping my default value in single quotes: image

Previously I was getting the error column "open" does not exist: image

This is downright baffling behavior.

(quoted from https://github.com/typeorm/typeorm/issues/2980)

Read more comments on GitHub >

github_iconTop Results From Across the Web

cannot use column reference in DEFAULT expression
The problem was that unlike in the example. ALTER TABLE example ALTER "ExampleID" SET DEFAULT nextval('example_id_seq').
Read more >
The jOOQ Release Note History
#14292, ForcedType enum with no matching value being converted to last enum value ... DEFAULT should act as STORED in client side computed...
Read more >
GraphQL specification
The following formal specification serves as a reference for those builders. It describes the language and its grammar, the type system and the...
Read more >
Hibernate ORM 5.4.33.Final User Guide - Red Hat on GitHub
Default value for a database column; 4.5. ... While traditionally, Hibernate used backticks for escaping SQL reserved keywords, JPA uses double quotes ...
Read more >
picocli - a mighty tiny command line interface
It is encouraged to use enum types for options or positional parameters with a limited set of valid values. Not only will picocli...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found