{"id":"1650718b8079c74e","repo":"sequelize/sequelize","slug":"unknown-attribute-options-sourcekey-passed-as-165071","errorCode":null,"errorMessage":"Unknown attribute \"${options.sourceKey}\" passed as sourceKey, define this attribute on model \"${source.name}\" first","messagePattern":"Unknown attribute \"(.+?)\" passed as sourceKey, define this attribute on model \"(.+?)\" first","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/associations/has-one.ts","lineNumber":98,"sourceCode":"   */\n  get sourceKeyAttribute(): SourceKey {\n    return this.sourceKey;\n  }\n\n  readonly inverse: BelongsToAssociation<T, S, TargetKey, SourceKey>;\n\n  readonly accessors: SingleAssociationAccessors;\n\n  constructor(\n    secret: symbol,\n    source: ModelStatic<S>,\n    target: ModelStatic<T>,\n    options: NormalizedHasOneOptions<SourceKey, TargetKey>,\n    parent?: Association,\n    inverse?: BelongsToAssociation<T, S, TargetKey, SourceKey>,\n  ) {\n    if (options?.sourceKey && !source.getAttributes()[options.sourceKey]) {\n      throw new Error(\n        `Unknown attribute \"${options.sourceKey}\" passed as sourceKey, define this attribute on model \"${source.name}\" first`,\n      );\n    }\n\n    if ('keyType' in options) {\n      throw new TypeError(\n        `Option \"keyType\" has been removed from the BelongsTo's options. Set \"foreignKey.type\" instead.`,\n      );\n    }\n\n    super(secret, source, target, options, parent);\n\n    this.inverse =\n      inverse ??\n      BelongsToAssociation.associate(\n        secret,\n        target,\n        source,","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/sequelize/sequelize/blob/7e1deec499d5afbb8d1877c2f4d545cead1214ec/packages/core/src/associations/has-one.ts#L80-L116","documentation":"In the HasOne constructor (has-one.ts:97), Sequelize verifies that an explicit `sourceKey` exists as an attribute on the source model. The sourceKey is the column the target's foreign key will reference (defaults to the source primary key). An unknown name throws an Error.","triggerScenarios":"`User.hasOne(Profile, { sourceKey: 'uid' })` where `User` has no `uid` attribute.","commonSituations":"Using an alternate unique key for the association but forgetting to define it on the source; typo; referencing a target-side attribute by mistake.","solutions":["Define the attribute on the source model.","Use the correct existing attribute, or omit `sourceKey` to use the source primary key."],"exampleFix":"// before\nUser.hasOne(Profile, { sourceKey: 'uid' }); // User has no 'uid'\n\n// after\nconst User = sequelize.define('User', {\n  uid: { type: DataTypes.UUID, unique: true },\n});\nUser.hasOne(Profile, { sourceKey: 'uid' });","handlingStrategy":"validation","validationCode":"function assertSourceKeyExists(sourceModel, sourceKey) {\n  if (sourceKey && !sourceModel.getAttributes()[sourceKey]) {\n    throw new Error(`sourceKey \"${sourceKey}\" does not exist on ${sourceModel.name}`);\n  }\n}\nassertSourceKeyExists(User, opts.sourceKey);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Omit `sourceKey` when using the primary key.","Define alternate source keys before referencing them.","Search `sourceKey:` usages when renaming source columns."],"tags":["has-one","source-key","unknown-attribute","validation"],"analyzedSha":"7e1deec499d5afbb8d1877c2f4d545cead1214ec","analyzedAt":"2026-08-03T18:58:44.549Z","schemaVersion":2}