Skip to content

generatedType column's insert option should be false by default #11509

Open
@VirtualPirate

Description

@VirtualPirate

Feature Description

I have a STORED generated column in PostgreSQL

  @Column({
    name: "is_opening_adjustment",
    type: "boolean",
    generatedType: "STORED",
    nullable: false,
    asExpression: `
      workspace_transaction_id IS NULL AND
      bill_id IS NULL AND
      invoice_id IS NULL AND
      manual_entry_id IS NULL AND
      fixed_asset_id IS NULL
    `,
  })
  isOpeningAdjustment?: boolean;

Now, whenever I try to INSERT some rows into this table this error occurs

QueryFailedError: cannot insert a non-DEFAULT value into column "is_opening_adjustment"

This occurs because typeorm is trying to insert a value into the generated column, which is not possible. Now, the solution to this is to explicitly specify, that during inserts this column should be omitted. Like this

  @Column({
    name: "is_opening_adjustment",
    type: "boolean",
    generatedType: "STORED",
    insert: false, // Important for generated columns
    update: false, // Important for generated columns
    select: true, // Include in selects by default
    nullable: false,
    asExpression: `
      workspace_transaction_id IS NULL AND
      bill_id IS NULL AND
      invoice_id IS NULL AND
      manual_entry_id IS NULL AND
      fixed_asset_id IS NULL
    `,
  })
  isOpeningAdjustment?: boolean;

But, this solution is unintuitive, GENERATED columns should never be a part of INSERT statements

The Solution

The solution for this is, while the user is passing the option for the @column() decorator, there will be an if statement, that will set the options.insert = false. So, that the generated columns are omitted during insertion queries.

Considered Alternatives

inset defaults to false, if generatedType is STORED

Additional Context

No response

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, and I know how to start.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions