{"id":"faf3050ed4c87ab1","repo":"sequelize/sequelize","slug":"you-have-defined-two-associations-with-the-same-na","errorCode":null,"errorMessage":"You have defined two associations with the same name \"${as}\" on the model \"${source.name}\". Use another alias using the \"as\" parameter.","messagePattern":"You have defined two associations with the same name \"(.+?)\" on the model \"(.+?)\"\\. Use another alias using the \"as\" parameter\\.","errorType":"exception","errorClass":"AssociationError","httpStatus":null,"severity":"error","filePath":"packages/core/src/associations/helpers.ts","lineNumber":107,"sourceCode":"  const existingAssociation = source.associations[as];\n  if (!existingAssociation) {\n    return;\n  }\n\n  const incompatibilityStatus = getAssociationsIncompatibilityStatus(\n    existingAssociation,\n    type,\n    target,\n    options,\n  );\n  if ((parent || existingAssociation.parentAssociation) && incompatibilityStatus == null) {\n    return;\n  }\n\n  const existingRoot = existingAssociation.rootAssociation;\n\n  if (!parent && existingRoot === existingAssociation) {\n    throw new AssociationError(\n      `You have defined two associations with the same name \"${as}\" on the model \"${source.name}\". Use another alias using the \"as\" parameter.`,\n    );\n  }\n\n  throw new AssociationError(\n    `\n${parent ? `The association \"${parent.as}\" needs to define` : `You are trying to define`} the ${type.name} association \"${options.as}\" from ${source.name} to ${target.name},\nbut that child association has already been defined as ${existingAssociation.associationType}, to ${target.name} by this call:\n\n${existingRoot.source.name}.${lowerFirst(existingRoot.associationType)}(${existingRoot.target.name}, ${NodeUtils.inspect(existingRoot.options)})\n\nThat association would be re-used if compatible, but it is incompatible because ${\n      incompatibilityStatus === IncompatibilityStatus.DIFFERENT_TYPES\n        ? `their types are different (${type.name} vs ${existingAssociation.associationType})`\n        : incompatibilityStatus === IncompatibilityStatus.DIFFERENT_TARGETS\n          ? `they target different models (${target.name} vs ${existingAssociation.target.name})`\n          : `their options are not reconcilable:\n","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/sequelize/sequelize/blob/7e1deec499d5afbb8d1877c2f4d545cead1214ec/packages/core/src/associations/helpers.ts#L89-L125","documentation":"Thrown by assertAssociationUnique when a second top-level association (no parent) is declared on the same source model with an identical 'as' alias, and the existing one is the root association. Sequelize keys associations by alias in source.associations, so a duplicate alias would overwrite the previous registration. The guard short-circuits only when the duplicate is fully compatible and nested under a parent; an unparented exact duplicate is always rejected.","triggerScenarios":"Calling e.g. User.hasMany(Task, { as: 'tasks' }) twice, or two association declarations (any type) whose normalized 'as' resolves to the same string. Occurs when 'as' is omitted and two targets share the same singular/plural name, or when copy-pasting an association block.","commonSituations":"Defining both a hasMany and a belongsTo that resolve to the same alias. Splitting model setup across files and re-declaring an association. Forgetting that omitting 'as' derives it from the target model name, causing collisions when two targets share names.","solutions":["Give each association a distinct 'as' alias.","Remove the redundant duplicate association declaration.","If two targets legitimately share a name, alias at least one explicitly with 'as'.","Grep for the alias in the model's association declarations to find the duplicate."],"exampleFix":"// before\nUser.hasMany(Task, { as: 'tasks' });\nUser.hasMany(Task, { as: 'tasks' }); // duplicate\n\n// after\nUser.hasMany(Task, { as: 'tasks' });\nUser.hasMany(Task, { as: 'assignedTasks' });","handlingStrategy":"validation","validationCode":"function assertAliasUnique(source: ModelStatic<any>, as: string): void {\n  if (source.associations[as]) {\n    throw new Error(`Alias '${as}' already declared on ${source.name}`);\n  }\n}\n\nassertAliasUnique(User, 'tasks');\nUser.hasMany(Task, { as: 'tasks' });","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Maintain a single setup function per model that declares all associations, so duplicates are visually obvious.","Lint for repeated 'as' values across association calls on the same source.","Prefer explicit 'as' over derived aliases to avoid accidental collisions."],"tags":["associations","naming","duplicate","sequelize-core"],"analyzedSha":"7e1deec499d5afbb8d1877c2f4d545cead1214ec","analyzedAt":"2026-08-03T18:58:44.549Z","schemaVersion":2}