truncate.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import type {
  2. CreationOptional,
  3. InferAttributes,
  4. InferCreationAttributes,
  5. Model,
  6. } from '@sequelize/core';
  7. import { DataTypes } from '@sequelize/core';
  8. import { expect } from 'chai';
  9. import { beforeAll2, sequelize, setResetMode } from '../support';
  10. interface IA extends Model<InferAttributes<IA>, InferCreationAttributes<IA>> {
  11. id: CreationOptional<number>;
  12. bId: number | null;
  13. }
  14. interface IB extends Model<InferAttributes<IB>, InferCreationAttributes<IB>> {
  15. id: CreationOptional<number>;
  16. aId: number | null;
  17. }
  18. describe('Sequelize#truncate', () => {
  19. setResetMode('destroy');
  20. const vars = beforeAll2(async () => {
  21. const A = sequelize.define<IA>('A', {
  22. id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  23. bId: { type: DataTypes.INTEGER },
  24. });
  25. const B = sequelize.define<IB>('B', {
  26. id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
  27. aId: { type: DataTypes.INTEGER },
  28. });
  29. // These models both have a foreign key that references the other model.
  30. // Sequelize should be able to create them.
  31. A.belongsTo(B, { foreignKey: { allowNull: true } });
  32. B.belongsTo(A, { foreignKey: { allowNull: false } });
  33. await sequelize.sync();
  34. return { A, B };
  35. });
  36. if (sequelize.dialect.supports.truncate.cascade) {
  37. it('supports truncating cyclic associations with { cascade: true }', async () => {
  38. const { A, B } = vars;
  39. await sequelize.transaction(async transaction => {
  40. const a = await A.create(
  41. {
  42. bId: null,
  43. },
  44. { transaction },
  45. );
  46. const b = await B.create(
  47. {
  48. aId: a.id,
  49. },
  50. { transaction },
  51. );
  52. a.bId = b.id;
  53. await a.save({ transaction });
  54. });
  55. // drop both tables
  56. await sequelize.truncate({ cascade: true });
  57. expect(await A.count()).to.eq(0);
  58. expect(await B.count()).to.eq(0);
  59. });
  60. }
  61. if (sequelize.dialect.supports.constraints.foreignKeyChecksDisableable) {
  62. it('supports truncating cyclic associations with { withoutForeignKeyChecks: true }', async () => {
  63. const { A, B } = vars;
  64. await sequelize.transaction(async transaction => {
  65. const a = await A.create(
  66. {
  67. bId: null,
  68. },
  69. { transaction },
  70. );
  71. const b = await B.create(
  72. {
  73. aId: a.id,
  74. },
  75. { transaction },
  76. );
  77. a.bId = b.id;
  78. await a.save({ transaction });
  79. });
  80. // drop both tables
  81. await sequelize.truncate({ withoutForeignKeyChecks: true });
  82. expect(await A.count()).to.eq(0);
  83. expect(await B.count()).to.eq(0);
  84. });
  85. }
  86. });