@@ -1690,7 +1690,10 @@ describe('association', () => {
16901690 oneToOneTestSuites ( User2 , Address2 ) ;
16911691 } ) ;
16921692
1693- function oneToOneWithOptionsTestSuites ( User : typeof Model , Address : typeof Model , alternateName : boolean = false ) : void {
1693+ function oneToOneWithOptionsTestSuites ( User : typeof Model ,
1694+ Address : typeof Model ,
1695+ alternateName : boolean = false ,
1696+ onDeleteAction : string = 'CASCADE' ) : void {
16941697 const foreignKey = alternateName ? 'user_id' : 'userId' ;
16951698 beforeEach ( ( ) => {
16961699 sequelize . addModels ( [ User , Address ] ) ;
@@ -1712,7 +1715,7 @@ describe('association', () => {
17121715 . to . have . property ( 'associations' )
17131716 . that . has . property ( 'address' )
17141717 . that . has . property ( 'options' )
1715- . with . property ( 'onDelete' , 'CASCADE' )
1718+ . with . property ( 'onDelete' , onDeleteAction )
17161719 ;
17171720
17181721 expect ( Address )
@@ -1728,7 +1731,7 @@ describe('association', () => {
17281731 . to . have . property ( 'associations' )
17291732 . that . has . property ( 'user' )
17301733 . that . has . property ( 'options' )
1731- . with . property ( 'onDelete' , 'CASCADE' )
1734+ . with . property ( 'onDelete' , onDeleteAction )
17321735 ;
17331736 } ) ;
17341737
@@ -1750,13 +1753,15 @@ describe('association', () => {
17501753
17511754 describe ( 'resolve foreign keys automatically with association options' , ( ) => {
17521755
1756+ const ON_DELETE_ACTION = 'SET NULL' ;
1757+
17531758 @Table
17541759 class User3 extends Model < User3 > {
17551760
17561761 @Column
17571762 name : string ;
17581763
1759- @HasOne ( ( ) => Address3 , { foreignKey : { allowNull : false } , onDelete : 'CASCADE' } )
1764+ @HasOne ( ( ) => Address3 , { foreignKey : { allowNull : false } , onDelete : ON_DELETE_ACTION } )
17601765 address : any ;
17611766 }
17621767
@@ -1780,11 +1785,11 @@ describe('association', () => {
17801785 @Column
17811786 userId : number ;
17821787
1783- @BelongsTo ( ( ) => User3 , { foreignKey : { allowNull : false } , onDelete : 'CASCADE' } )
1788+ @BelongsTo ( ( ) => User3 , { foreignKey : { allowNull : false } , onDelete : ON_DELETE_ACTION } )
17841789 user : User3 ;
17851790 }
17861791
1787- oneToOneWithOptionsTestSuites ( User3 , Address3 ) ;
1792+ oneToOneWithOptionsTestSuites ( User3 , Address3 , false , ON_DELETE_ACTION ) ;
17881793 } ) ;
17891794
17901795 describe ( 'set foreign keys explicitly with association options' , ( ) => {
@@ -1814,10 +1819,6 @@ describe('association', () => {
18141819 @Column
18151820 country : string ;
18161821
1817- @ForeignKey ( ( ) => User4 )
1818- @Column ( { field : 'user_id' } )
1819- userId : number ;
1820-
18211822 @BelongsTo ( ( ) => User4 , {
18221823 foreignKey : { allowNull : false , name : 'user_id' } ,
18231824 onDelete : 'CASCADE' ,
@@ -1861,6 +1862,48 @@ describe('association', () => {
18611862
18621863 oneToOneTestSuites ( User5 , Address5 ) ;
18631864 } ) ;
1865+
1866+ describe ( 'set foreign keys explicitly with association options (allowNull: false on foreignKey)' , ( ) => {
1867+
1868+ @Table
1869+ class User6 extends Model < User6 > {
1870+
1871+ @Column
1872+ name : string ;
1873+
1874+ @HasOne ( ( ) => Address6 , { foreignKey : { allowNull : false } } )
1875+ address : any ;
1876+ }
1877+
1878+ @Table
1879+ class Address6 extends Model < Address6 > {
1880+
1881+ @Column
1882+ street : string ;
1883+
1884+ @Column
1885+ zipCode : string ;
1886+
1887+ @Column
1888+ city : string ;
1889+
1890+ @Column
1891+ country : string ;
1892+
1893+ @ForeignKey ( ( ) => User6 )
1894+ @AllowNull ( false )
1895+ @Column ( { field : 'user_id' } )
1896+ userId : number ;
1897+
1898+ @BelongsTo ( ( ) => User6 , {
1899+ onDelete : 'CASCADE' ,
1900+ foreignKey : { allowNull : false }
1901+ } )
1902+ user : User6 ;
1903+ }
1904+
1905+ oneToOneWithOptionsTestSuites ( User6 , Address6 , false ) ;
1906+ } ) ;
18641907 } ) ;
18651908
18661909} ) ;
0 commit comments