@@ -24,6 +24,10 @@ import { Guest as GuestV1 } from "./entity/v1/Guest"
2424import { Comment as CommentV2 } from "./entity/v2/Comment"
2525import { Guest as GuestV2 } from "./entity/v2/Guest"
2626import { View } from "./entity/View"
27+ import { Professor } from "./entity/Professor"
28+ import { Subject } from "./entity/Subject"
29+ import { SiteLocation } from "./entity/SiteLocation"
30+ import { Site } from "./entity/Site"
2731
2832describe ( "Connection" , ( ) => {
2933 // const resourceDir = __dirname + "/../../../../../test/functional/connection/";
@@ -414,4 +418,81 @@ describe("Connection", () => {
414418 } ) ,
415419 ) )
416420 } )
421+
422+ // GitHub issue #7738
423+ describe ( "synchronize with multiple foreign keys to same table" , ( ) => {
424+ let connections : DataSource [ ]
425+ beforeEach (
426+ async ( ) =>
427+ ( connections = await createTestingConnections ( {
428+ enabledDrivers : [ "postgres" ] ,
429+ entities : [ Professor , Subject , Site , SiteLocation ] ,
430+ dropSchema : true ,
431+ } ) ) ,
432+ )
433+ afterEach ( ( ) => closeTestingConnections ( connections ) )
434+
435+ it ( "should not fail with constraint already exists error when synchronizing multiple times" , ( ) =>
436+ Promise . all (
437+ connections . map ( async ( dataSource ) => {
438+ // First synchronization
439+ await dataSource . synchronize ( )
440+
441+ // Second synchronization should not fail with constraint error
442+ await expect ( dataSource . synchronize ( ) ) . to . not . be . rejected
443+
444+ const professorRepo = dataSource . getRepository ( Professor )
445+ const subjectRepo = dataSource . getRepository ( Subject )
446+ const siteRepo = dataSource . getRepository ( Site )
447+ const siteLocationRepo =
448+ dataSource . getRepository ( SiteLocation )
449+
450+ // Create and save entities to test foreign key relationships
451+ const professor = professorRepo . create ( {
452+ name : "Dr. Smith" ,
453+ } )
454+ await professorRepo . save ( professor )
455+
456+ const assistant = professorRepo . create ( {
457+ name : "Dr. Jones" ,
458+ } )
459+ await professorRepo . save ( assistant )
460+
461+ const subject = subjectRepo . create ( {
462+ name : "Mathematics" ,
463+ professor : professor ,
464+ assistant : assistant ,
465+ } )
466+ await subjectRepo . save ( subject )
467+
468+ const loadedSubject = await subjectRepo . findOne ( {
469+ where : { id : subject . id } ,
470+ relations : [ "professor" , "assistant" ] ,
471+ } )
472+
473+ const site = siteRepo . create ( { name : "Main Campus" } )
474+ await siteRepo . save ( site )
475+
476+ const siteLocation = siteLocationRepo . create ( {
477+ address : "123 Main St" ,
478+ site : site ,
479+ } )
480+ await siteLocationRepo . save ( siteLocation )
481+
482+ const loadedSiteLocation = await siteLocationRepo . findOne ( {
483+ where : { id : siteLocation . id } ,
484+ relations : [ "site" ] ,
485+ } )
486+
487+ expect ( loadedSiteLocation ) . to . not . be . null
488+ expect ( loadedSiteLocation ! . site . name ) . to . equal (
489+ "Main Campus" ,
490+ )
491+
492+ expect ( loadedSubject ) . to . not . be . null
493+ expect ( loadedSubject ! . professor . name ) . to . equal ( "Dr. Smith" )
494+ expect ( loadedSubject ! . assistant . name ) . to . equal ( "Dr. Jones" )
495+ } ) ,
496+ ) )
497+ } )
417498} )
0 commit comments