1- import isEqual from 'lodash/isEqual ' ;
1+ import { MapView } from '@sequelize/utils ' ;
22import isEmpty from 'lodash/isEmpty.js' ;
3+ import isEqual from 'lodash/isEqual' ;
34import isObject from 'lodash/isObject.js' ;
45import upperFirst from 'lodash/upperFirst' ;
56import assert from 'node:assert' ;
@@ -29,7 +30,6 @@ import { HasManyAssociation } from './has-many.js';
2930import { HasOneAssociation } from './has-one.js' ;
3031import type { NormalizeBaseAssociationOptions } from './helpers' ;
3132import { defineAssociation , mixinMethods , normalizeBaseAssociationOptions } from './helpers' ;
32- import { MapView } from '@sequelize/utils' ;
3333
3434/**
3535 * One-to-one association
@@ -64,7 +64,7 @@ export class BelongsToAssociation<
6464
6565 foreignKey : SourceKey ;
6666
67- foreignKeys : Array < { source : SourceKey , target : TargetKey } > = [ ] ;
67+ foreignKeys : Array < { source : SourceKey ; target : TargetKey } > = [ ] ;
6868
6969 /**
7070 * The column name of the foreign key
@@ -113,26 +113,29 @@ export class BelongsToAssociation<
113113 parent ?: Association ,
114114 ) {
115115 const isForeignKeyEmpty = isEmpty ( options . foreignKey ) ;
116- const isForeignKeysValid = Array . isArray ( options . foreignKeys )
117- && options . foreignKeys . length > 0
118- && options . foreignKeys . every ( fk => ! isEmpty ( fk ) ) ;
116+ const isForeignKeysValid =
117+ Array . isArray ( options . foreignKeys ) &&
118+ options . foreignKeys . length > 0 &&
119+ options . foreignKeys . every ( fk => ! isEmpty ( fk ) ) ;
119120
120121 let targetKeys ;
121122 if ( isForeignKeyEmpty && isForeignKeysValid ) {
122- targetKeys = ( options . foreignKeys as Array < { source : SourceKey , target : TargetKey } > ) . map ( fk => fk . target ) ;
123+ targetKeys = ( options . foreignKeys as Array < { source : SourceKey ; target : TargetKey } > ) . map (
124+ fk => fk . target ,
125+ ) ;
123126 } else {
124127 targetKeys = options ?. targetKey
125- ? [ options . targetKey ]
126- : target . modelDefinition . primaryKeysAttributeNames ;
128+ ? [ options . targetKey ]
129+ : target . modelDefinition . primaryKeysAttributeNames ;
127130 }
128131
129132 const targetAttributes = target . modelDefinition . attributes ;
130133
131134 for ( const key of targetKeys ) {
132135 if ( ! targetAttributes . has ( key ) ) {
133136 throw new Error (
134- `Unknown attribute "${ key } " passed as targetKey, define this attribute on model "${ target . name } " first` ,
135- ) ;
137+ `Unknown attribute "${ key } " passed as targetKey, define this attribute on model "${ target . name } " first` ,
138+ ) ;
136139 }
137140 }
138141
@@ -148,15 +151,14 @@ export class BelongsToAssociation<
148151
149152 const shouldHashPrimaryKey = this . shouldHashPrimaryKey ( targetAttributes ) ;
150153
151- if ( ( ! isEmpty ( options . foreignKeys ) && isEmpty ( options . foreignKey ) ) && ! shouldHashPrimaryKey ) {
152-
154+ if ( ! isEmpty ( options . foreignKeys ) && isEmpty ( options . foreignKey ) && ! shouldHashPrimaryKey ) {
153155 // Composite key flow
154156 // TODO: fix this
155157 this . targetKey = null as any ;
156158 this . foreignKey = null as any ;
157159 this . identifierField = null as any ;
158160
159- this . foreignKeys = options . foreignKeys as Array < { source : SourceKey , target : TargetKey } > ;
161+ this . foreignKeys = options . foreignKeys as Array < { source : SourceKey ; target : TargetKey } > ;
160162
161163 for ( const targetKey of this . targetKeys ) {
162164 const targetColumn = targetAttributes . get ( targetKey ) ! ;
@@ -170,7 +172,6 @@ export class BelongsToAssociation<
170172 [ targetColumn . columnName ] : newForeignKeyAttribute ,
171173 } ) ;
172174 }
173-
174175 } else {
175176 const [ targetKey ] = this . targetKeys ;
176177 this . targetKey = targetKey ;
@@ -179,9 +180,9 @@ export class BelongsToAssociation<
179180 // else, server throws SQL0573N error. Hence, setting it here explicitly
180181 // for non primary columns.
181182 if (
182- target . sequelize . dialect . name === 'db2' &&
183- targetAttributes . get ( this . targetKey ) ! . primaryKey !== true
184- ) {
183+ target . sequelize . dialect . name === 'db2' &&
184+ targetAttributes . get ( this . targetKey ) ! . primaryKey !== true
185+ ) {
185186 // TODO: throw instead
186187 this . target . modelDefinition . rawAttributes [ this . targetKey ] . unique = true ;
187188 }
@@ -218,7 +219,7 @@ export class BelongsToAssociation<
218219 if ( options . foreignKeyConstraints !== false ) {
219220 const existingReference = existingForeignKey ?. references
220221 ? ( ( normalizeReference ( existingForeignKey . references ) ??
221- existingForeignKey . references ) as AttributeReferencesOptions )
222+ existingForeignKey . references ) as AttributeReferencesOptions )
222223 : undefined ;
223224
224225 const queryGenerator = this . source . sequelize . queryGenerator ;
@@ -233,24 +234,27 @@ export class BelongsToAssociation<
233234 if ( existingReferencedTable ) {
234235 if ( ! isEqual ( existingReferencedTable , newReferencedTable ) ) {
235236 throw new Error (
236- `Foreign key ${ this . foreignKey } on ${ this . source . name } already references ${ queryGenerator . quoteTable ( existingReferencedTable ) } , but this association needs to make it reference ${ queryGenerator . quoteTable ( newReferencedTable ) } instead.` ,
237- ) ;
237+ `Foreign key ${ this . foreignKey } on ${ this . source . name } already references ${ queryGenerator . quoteTable ( existingReferencedTable ) } , but this association needs to make it reference ${ queryGenerator . quoteTable ( newReferencedTable ) } instead.` ,
238+ ) ;
238239 }
239240 } else {
240241 newReference . table = newReferencedTable ;
241242 }
242243
243- if ( existingReference ?. key && existingReference . key !== this . targetKeyField ( this . targetKey ) ) {
244- throw new Error (
245- `Foreign key ${ this . foreignKey } on ${ this . source . name } already references column ${ existingReference . key } , but this association needs to make it reference ${ this . targetKeyField } instead.` ,
246- ) ;
247- }
244+ if (
245+ existingReference ?. key &&
246+ existingReference . key !== this . targetKeyField ( this . targetKey )
247+ ) {
248+ throw new Error (
249+ `Foreign key ${ this . foreignKey } on ${ this . source . name } already references column ${ existingReference . key } , but this association needs to make it reference ${ this . targetKeyField } instead.` ,
250+ ) ;
251+ }
248252
249253 newReference . key = this . targetKeyField ( this . targetKey ) ;
250254
251255 newForeignKeyAttribute . references = newReference ;
252256 newForeignKeyAttribute . onDelete ??=
253- newForeignKeyAttribute . allowNull !== false ? 'SET NULL' : 'CASCADE' ;
257+ newForeignKeyAttribute . allowNull !== false ? 'SET NULL' : 'CASCADE' ;
254258 newForeignKeyAttribute . onUpdate ??= newForeignKeyAttribute . onUpdate ?? 'CASCADE' ;
255259 }
256260
@@ -300,15 +304,21 @@ export class BelongsToAssociation<
300304 }
301305 }
302306
303- private setupTargetKeys ( options : NormalizedBelongsToOptions < SourceKey , TargetKey > , target : ModelStatic < T > ) {
307+ private setupTargetKeys (
308+ options : NormalizedBelongsToOptions < SourceKey , TargetKey > ,
309+ target : ModelStatic < T > ,
310+ ) {
304311 const isForeignKeyEmpty = isEmpty ( options . foreignKey ) ;
305- const isForeignKeysValid = Array . isArray ( options . foreignKeys )
306- && options . foreignKeys . length > 0
307- && options . foreignKeys . every ( fk => ! isEmpty ( fk ) ) ;
312+ const isForeignKeysValid =
313+ Array . isArray ( options . foreignKeys ) &&
314+ options . foreignKeys . length > 0 &&
315+ options . foreignKeys . every ( fk => ! isEmpty ( fk ) ) ;
308316
309317 let targetKeys ;
310318 if ( isForeignKeyEmpty && isForeignKeysValid ) {
311- targetKeys = ( options . foreignKeys as Array < { source : SourceKey , target : TargetKey } > ) . map ( fk => fk . target ) ;
319+ targetKeys = ( options . foreignKeys as Array < { source : SourceKey ; target : TargetKey } > ) . map (
320+ fk => fk . target ,
321+ ) ;
312322 } else {
313323 targetKeys = options ?. targetKey
314324 ? [ options . targetKey ]
@@ -318,11 +328,15 @@ export class BelongsToAssociation<
318328 const targetAttributes = target . modelDefinition . attributes ;
319329 for ( const key of targetKeys ) {
320330 if ( ! targetAttributes . has ( key ) ) {
321- throw new Error ( `Unknown attribute "${ key } " passed as targetKey, define this attribute on model "${ target . name } " first` ) ;
331+ throw new Error (
332+ `Unknown attribute "${ key } " passed as targetKey, define this attribute on model "${ target . name } " first` ,
333+ ) ;
322334 }
323335 }
324336
325- this . targetKeys = Array . isArray ( targetKeys ) ? targetKeys : [ ...targetKeys ] . map ( key => key as TargetKey ) ;
337+ this . targetKeys = Array . isArray ( targetKeys )
338+ ? targetKeys
339+ : [ ...targetKeys ] . map ( key => key as TargetKey ) ;
326340 }
327341
328342 /**
@@ -334,15 +348,20 @@ export class BelongsToAssociation<
334348 * @param targetAttributes
335349 * @protected
336350 */
337- protected shouldHashPrimaryKey ( targetAttributes : MapView < string , NormalizedAttributeOptions < Model < any , any > > > ) : Boolean {
351+ protected shouldHashPrimaryKey (
352+ targetAttributes : MapView < string , NormalizedAttributeOptions < Model < any , any > > > ,
353+ ) : Boolean {
338354 const primaryKeyAttributes = [ ] ;
339355 for ( const attributes of targetAttributes . values ( ) ) {
340356 if ( attributes . primaryKey ) {
341357 primaryKeyAttributes . push ( attributes ) ;
342358 }
343359 }
344360
345- return primaryKeyAttributes . length === 2 && primaryKeyAttributes . some ( attr => attr . type === 'BINARY(16)' ) ;
361+ return (
362+ primaryKeyAttributes . length === 2 &&
363+ primaryKeyAttributes . some ( attr => attr . type === 'BINARY(16)' )
364+ ) ;
346365 }
347366
348367 static associate <
@@ -456,13 +475,13 @@ export class BelongsToAssociation<
456475 } else if ( this . targetKeyIsPrimary ( this . targetKey ) && ! options . where ) {
457476 const foreignKeyValue = instances [ 0 ] . get ( this . foreignKey ) ;
458477
459- return Target . findByPk ( foreignKeyValue as any , options ) ; } else {
478+ return Target . findByPk ( foreignKeyValue as any , options ) ;
479+ } else {
460480 // TODO: combine once we can just have the foreignKey in the foreignKeys array all the time
461481 if ( this . isCompositeKey ) {
462482 for ( const key of this . foreignKeys ) {
463483 where [ key . target ] = instances [ 0 ] . get ( key . source ) ;
464484 }
465-
466485 } else {
467486 where [ this . targetKey ] = instances [ 0 ] . get ( this . foreignKey ) ;
468487 }
0 commit comments