A
A
Alexander2021-09-22 12:59:37
PostgreSQL
Alexander, 2021-09-22 12:59:37

How to delete records from a table linked by key?

After deletion , the User,linked user idin Tokenthe table was deleted

const User = sequelize.define('user', {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  email: { type: DataTypes.STRING, unique: true, allowNull: false },
  password: { type: DataTypes.STRING, allowNull: false },
  firstName: { type: DataTypes.STRING, allowNull: false },
  lastName: { type: DataTypes.STRING, allowNull: false },
  dateOfBirth: { type: DataTypes.DATEONLY, allowNull: false },
  country: { type: DataTypes.STRING, allowNull: false },
  gender: { type: DataTypes.STRING, allowNull: false },
  subscribe: { type: DataTypes.BOOLEAN },
  isActivated: { type: DataTypes.BOOLEAN, defaultValue: false },
  activationLink: { type: DataTypes.STRING, allowNull: false },
  role: { type: DataTypes.STRING, defaultValue: 'USER' },
})

const Token = sequelize.define('token', {
  id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  user: {
    type: DataTypes.INTEGER,
    references: { model: 'User', key: 'id' },
  },
  refreshToken: { type: DataTypes.STRING, allowNull: false },
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Tsvetkov, 2021-09-22
@tsklab

...
user: {
    type: DataTypes.INTEGER,
    references: { model: 'User', key: 'id' }, onDelete: 'CASCADE',
  },
...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question