association.test.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { AssociationError } from '@sequelize/core';
  2. import { expect } from 'chai';
  3. import { getTestDialectTeaser, sequelize } from '../../support';
  4. describe(getTestDialectTeaser('belongsTo'), () => {
  5. it('should throw an AssociationError when two associations have the same alias', () => {
  6. const User = sequelize.define('User');
  7. const Task = sequelize.define('Task');
  8. User.belongsTo(Task, { as: 'task' });
  9. expect(() => {
  10. User.belongsTo(Task, { as: 'task' });
  11. }).to.throw(
  12. AssociationError,
  13. 'You have defined two associations with the same name "task" on the model "User". Use another alias using the "as" parameter.',
  14. );
  15. });
  16. it('should throw an AssociationError when two associations have the same alias (one inferred)', () => {
  17. const User = sequelize.define('User');
  18. const Task = sequelize.define('Task');
  19. const association = User.belongsTo(Task);
  20. expect(association.as).to.eq('task');
  21. expect(() => {
  22. User.belongsTo(Task, { as: 'task' });
  23. }).to.throw(
  24. AssociationError,
  25. 'You have defined two associations with the same name "task" on the model "User". Use another alias using the "as" parameter.',
  26. );
  27. });
  28. it('should throw an AssociationError when two associations have the same alias (both inferred)', () => {
  29. const User = sequelize.define('User');
  30. const Task = sequelize.define('Task');
  31. User.belongsTo(Task);
  32. expect(() => {
  33. User.belongsTo(Task);
  34. }).to.throw(
  35. AssociationError,
  36. 'You have defined two associations with the same name "task" on the model "User". Use another alias using the "as" parameter.',
  37. );
  38. });
  39. });