drop.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import type { ReferentialAction } from '@sequelize/core';
  2. import { DataTypes, Deferrable } from '@sequelize/core';
  3. import { sequelize } from '../support';
  4. const dialect = sequelize.dialect.name;
  5. describe('Sequelize#drop', () => {
  6. it('supports dropping cyclic associations', async () => {
  7. const A = sequelize.define('A', {
  8. bId: {
  9. type: DataTypes.INTEGER,
  10. references: {
  11. deferrable: Deferrable.INITIALLY_IMMEDIATE,
  12. },
  13. },
  14. });
  15. const B = sequelize.define('B', {
  16. aId: {
  17. type: DataTypes.INTEGER,
  18. references: {
  19. deferrable: Deferrable.INITIALLY_IMMEDIATE,
  20. },
  21. },
  22. });
  23. // mssql refuses cyclic references unless ON DELETE and ON UPDATE is set to NO ACTION
  24. const mssqlConstraints =
  25. dialect === 'mssql'
  26. ? { onDelete: 'NO ACTION' as ReferentialAction, onUpdate: 'NO ACTION' as ReferentialAction }
  27. : null;
  28. // These models both have a foreign key that references the other model.
  29. // Sequelize should be able to create them.
  30. A.belongsTo(B, { foreignKey: { allowNull: false, ...mssqlConstraints } });
  31. B.belongsTo(A, { foreignKey: { allowNull: false, ...mssqlConstraints } });
  32. await sequelize.sync();
  33. // drop both tables
  34. await sequelize.drop();
  35. });
  36. it('supports dropping cyclic associations with { cascade: true } in supported dialects', async () => {
  37. if (!sequelize.dialect.supports.dropTable.cascade) {
  38. return;
  39. }
  40. const A = sequelize.define('A', {
  41. bId: {
  42. type: DataTypes.INTEGER,
  43. },
  44. });
  45. const B = sequelize.define('B', {
  46. aId: {
  47. type: DataTypes.INTEGER,
  48. },
  49. });
  50. // These models both have a foreign key that references the other model.
  51. // Sequelize should be able to create them.
  52. A.belongsTo(B, { foreignKey: { allowNull: false } });
  53. B.belongsTo(A, { foreignKey: { allowNull: false } });
  54. await sequelize.sync();
  55. // drop both tables
  56. await sequelize.drop({ cascade: true });
  57. });
  58. });