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

How to create check constraint?

Open akoidan opened this issue 3 years ago • 3 comments

Issue

No way to create a constraint rather than unique, index, or unique together Expected behaviour when running sync({force: true}}:

import {
  BelongsTo,
  Column,
  DataType,
  ForeignKey,
  Model,
  Table,
} from 'sequelize-typescript';


@Table({ tableName: 'room', howToAddCustomConstrainHere: '???'})
export class RoomModel extends Model<RoomModel> {

  @Column({
    type: DataType.INTEGER,
    autoIncrement: true,
    primaryKey: true,
  })
  public id: number;


  @Column({
    type: DataType.STRING(16)
  })
  public name: string;

  @Column({
    type: DataType.INTEGER
  })
  public channelId: number;


}

Should produce check constrain.

create or replace table room
(
	id int auto_increment,
	name varchar(16) null,
	channel_id int null,
	constraint channel_should_exist_for_public_room_and_not_exist_for_private
		check (`channel_id` is null and `name` is null or `channel_id` is not n),
);

I searched thought documentation and there's no way

Issue type

  • [ ] bug report
  • [x] feature request

akoidan avatar Apr 10 '22 21:04 akoidan

I would also like to know how to add a CHECK constraint to a table.

Rich43 avatar Jun 05 '22 08:06 Rich43

I triple this, on a side note it seems that only migrations let you do this

Coinhexa avatar Oct 15 '22 08:10 Coinhexa

@akoidan @Rich43 I found it. This is the closest thing we have to check constraint it seems

slidenerd avatar Oct 20 '22 12:10 slidenerd