Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Default decorator throws error #718

Open
byronferguson opened this issue Dec 11, 2019 · 6 comments
Open

@Default decorator throws error #718

byronferguson opened this issue Dec 11, 2019 · 6 comments

Comments

@byronferguson
Copy link

byronferguson commented Dec 11, 2019

Versions

  • sequelize: 5.21.2
  • sequelize-typescript: 1.1.0
  • typescript: 3.7.3

I'm submitting a ...

[ x ] bug report
[ ] feature request

Actual behavior:
Error: @Column annotation is missing for "sort" of class "CampaignPhase" or annotation order is wrong.

Currently throwing an error. (does the same thing with @Comment() too

Expected behavior:
generate the DEFAULT_VALUE in the database engine

Related code:

@Table
export class CampaignPhase extends Model<CampaignPhase> {
  @Column
  name!: string;

  @Column
  @NotNull
  @Default(1)
  sort!: number;

  @ForeignKey(() => CampaignTemplate)
  @Column
  templateId!: number;

  @BelongsTo(() => CampaignTemplate)
  template?: CampaignTemplate;
}
@max10rogerio
Copy link

Try:

@table
export class CampaignPhase extends Model<CampaignPhase> {
  @Column
  name!: string;

  @NotNull
  @Default(1)
  @Column // in the last position
  sort!: number;

  @Foreignkey(() => CampaignTemplate)
  @Column
  templateId!: number;

  @BelongsTo(() => CampaignTemplate)
  template?: CampaignTemplate;
}

@byronferguson
Copy link
Author

byronferguson commented Dec 18, 2019 via email

@RobinBuschmann
Copy link
Member

Hey @byronferguson could you solve your issue?

@byronferguson
Copy link
Author

I was not able to solve the issue I raised.

@RobinBuschmann
Copy link
Member

@byronferguson could it be that you faced another issue after correcting the order of annotations? The following throws Error: Invalid definition for "CampaignPhase.sort", "notNull" validator is only allowed with "allowNull:false":

  @NotNull
  @Default(1)
  @Column
  sort!: number;

But when using this setup instead, it works very well:

  @AllowNull(true)
  @Default(1)
  @Column
  sort!: number;

Hope this helps!

@neokamikai
Copy link

I'm getting a sql syntax error using default value with dialect mssql:

{
    message: "Incorrect syntax near the keyword 'DEFAULT'.",
    code: 'EREQUEST',
    number: 156,
    state: 1,
    class: 15,
    serverName: 'xxxx',
    procName: '',
    lineNumber: 1,
    sql: 'ALTER TABLE [dbo].[tb_test] ALTER COLUMN [is_active] BIT NOT NULL DEFAULT 0;',
    parameters: undefined
  }

I think for mssql you should add a constraint no the column, not just alter column:

ALTER TABLE [dbo].[tb_test] ADD  CONSTRAINT [DF_tb_test_is_active]  DEFAULT ((0)) FOR [is_active]

I'm not sure if it's needed, but if so, before dropping a table with default constraints all default constraints should be dropped first. You can query them with:

SELECT * FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID('[dbo].[tb_test]')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants