0

I have a model in sequelize that stores like dislike votes. It needs to know which post was liked/disliked and by which user. I am using sequelize-typescript

likeDislike.model.ts

/* eslint import/no-cycle: "off" */
import { DataTypes } from 'sequelize';
import {
  Model,
  PrimaryKey,
  Column,
  Table,
  Default,
  IsUUID,
  BelongsTo,
  Unique,
} from 'sequelize-typescript';
import { Account, FeedItem } from 'data/models';

@Table({
  createdAt: false,
  freezeTableName: true,
  tableName: 'feed_item_like_dislike_votes',
  updatedAt: false,
})
export default class FeedItemLikeDislikeVote extends Model {
  @Column
  vote: boolean;

  @BelongsTo(() => Account, {
    foreignKey: { name: 'accountId', allowNull: false },
    as: 'account_',
  })
  @Column({ type: DataTypes.UUID, field: 'account_id' })
  account: Account;

  @BelongsTo(() => FeedItem, {
    foreignKey: { name: 'feedItemId', allowNull: false },
    as: 'feedItem_',
  })
  @Column({ type: DataTypes.UUID, field: 'feed_item_id' })
  feedItem: FeedItem;
}

How do I make a composite primary key (account, feedItem) such that it is also a foreign key to account and feedItem respectively?

PirateApp
  • 5,433
  • 4
  • 57
  • 90
  • 1
    AFAIK Sequelize dos not support composite FKs as I mentioned in the other question comments: https://stackoverflow.com/questions/73986092/how-to-handle-a-composite-primary-key-in-adminbro-adminjs?noredirect=1#comment130665426_73986092 – Anatoly Oct 09 '22 at 09:20
  • @Anatoly what about sequelize migrations? the models may not support a composite foreign key but what happens if you try putting one inside a migration file and then start using it – PirateApp Oct 09 '22 at 13:32
  • Well, models are used regardless what columns and table you have in DB after applying migrations. What you define in models is what you get (if the model was defined correctly). – Anatoly Oct 09 '22 at 17:22
  • @Anatoly but the actual query would use the DB created by the migration would it not? so if you have a pair of columns (user_id, account_id) which is not defined as a primary key in the model but as a primary key in the migration, the indexing still takes effect no? – PirateApp Oct 09 '22 at 17:26
  • 1
    Even if Sequelize will detect composite PK properly you can't indicate two fields in associations to link two tables by two fields. – Anatoly Oct 09 '22 at 17:30
  • @Anatoly so the only workaround is to have an id column with composite unique constraint i am guessing in both model and migration – PirateApp Oct 09 '22 at 18:02
  • 1
    If you want to guarantee that there will be no two records with the same account id and feed id then yes you need an unique index (not PK) on both fields. – Anatoly Oct 09 '22 at 19:17
  • @Anatoly thank you for the clarification, if the primary key was not composite would it work then? like having the same column as both primary key and foreign key in the model as well as migration – PirateApp Oct 10 '22 at 05:42
  • 1
    You need a separate surrogate PK field (id) and an unique index on a pair of `accountId` and `feedItemId` – Anatoly Oct 10 '22 at 18:57

0 Answers0