sequelize-typescript icon indicating copy to clipboard operation
sequelize-typescript copied to clipboard

Sequelize upsert with sequelize.literal does not work on Postgres

Open eldimious opened this issue 4 years ago • 1 comments

Issue

Versions

  • sequelize: 6.16.1
  • sequelize-typescript: 2.1.3
  • typescript: 4.5.4

Issue type

  • [x] bug report
  • [ ] feature request

Actual behavior

In the following example, I am trying to upsert a column with sequelize.literal. When I run the cmd i get invalid reference to FROM-clause entry for table \"projects\ compared when I run the same sequelize.literal using update. I suspect that the issue is related when tries to insert new row

Related code

import {
  Table,
  Column,
  DataType,
  PrimaryKey,
  Unique,
  AllowNull,
  NotEmpty,
  AutoIncrement,
  CreatedAt,
  UpdatedAt,
  Model,
  Default,
} from 'sequelize-typescript';

export interface IProject {
  id?: number;
  projectId: string;
  counter: number;
  createdAt?: Date;
  updatedAt?: Date;
}

@Table({
  timestamps: true,
  tableName: 'projects',
})
export class ProjectDao extends Model {
  @PrimaryKey
  @AutoIncrement
  @Column(DataType.INTEGER)
    id!: number;

  @AllowNull(false)
  @NotEmpty
  @Unique
  @Column(DataType.STRING)
    projectId!: string;

  @AllowNull(false)
  @NotEmpty
  @Default(1)
  @Column(DataType.INTEGER)
    counter!: number;
}

(async() => {
  await ProjectDao.upsert({
      projectId: 'test',
      counter: Sequelize.literal('"projects"."counter"+1'),
    }, {
      returning: true
    });
})();

When i run the above cmd i get: "invalid reference to FROM-clause entry for table \"projects\"" compared when i am using Sequelize.literal('"projects"."counter"+1') on a update method.

eldimious avatar Feb 18 '22 11:02 eldimious

Use the logging option to find out what the SQL looks like, it looks like the table name is not 'projects' in the SQL sequelize generates

ephys avatar Apr 07 '22 13:04 ephys