@@ -1690,7 +1690,10 @@ describe('association', () => {
1690
1690
oneToOneTestSuites ( User2 , Address2 ) ;
1691
1691
} ) ;
1692
1692
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 {
1694
1697
const foreignKey = alternateName ? 'user_id' : 'userId' ;
1695
1698
beforeEach ( ( ) => {
1696
1699
sequelize . addModels ( [ User , Address ] ) ;
@@ -1712,7 +1715,7 @@ describe('association', () => {
1712
1715
. to . have . property ( 'associations' )
1713
1716
. that . has . property ( 'address' )
1714
1717
. that . has . property ( 'options' )
1715
- . with . property ( 'onDelete' , 'CASCADE' )
1718
+ . with . property ( 'onDelete' , onDeleteAction )
1716
1719
;
1717
1720
1718
1721
expect ( Address )
@@ -1728,7 +1731,7 @@ describe('association', () => {
1728
1731
. to . have . property ( 'associations' )
1729
1732
. that . has . property ( 'user' )
1730
1733
. that . has . property ( 'options' )
1731
- . with . property ( 'onDelete' , 'CASCADE' )
1734
+ . with . property ( 'onDelete' , onDeleteAction )
1732
1735
;
1733
1736
} ) ;
1734
1737
@@ -1750,13 +1753,15 @@ describe('association', () => {
1750
1753
1751
1754
describe ( 'resolve foreign keys automatically with association options' , ( ) => {
1752
1755
1756
+ const ON_DELETE_ACTION = 'SET NULL' ;
1757
+
1753
1758
@Table
1754
1759
class User3 extends Model < User3 > {
1755
1760
1756
1761
@Column
1757
1762
name : string ;
1758
1763
1759
- @HasOne ( ( ) => Address3 , { foreignKey : { allowNull : false } , onDelete : 'CASCADE' } )
1764
+ @HasOne ( ( ) => Address3 , { foreignKey : { allowNull : false } , onDelete : ON_DELETE_ACTION } )
1760
1765
address : any ;
1761
1766
}
1762
1767
@@ -1780,11 +1785,11 @@ describe('association', () => {
1780
1785
@Column
1781
1786
userId : number ;
1782
1787
1783
- @BelongsTo ( ( ) => User3 , { foreignKey : { allowNull : false } , onDelete : 'CASCADE' } )
1788
+ @BelongsTo ( ( ) => User3 , { foreignKey : { allowNull : false } , onDelete : ON_DELETE_ACTION } )
1784
1789
user : User3 ;
1785
1790
}
1786
1791
1787
- oneToOneWithOptionsTestSuites ( User3 , Address3 ) ;
1792
+ oneToOneWithOptionsTestSuites ( User3 , Address3 , false , ON_DELETE_ACTION ) ;
1788
1793
} ) ;
1789
1794
1790
1795
describe ( 'set foreign keys explicitly with association options' , ( ) => {
@@ -1814,10 +1819,6 @@ describe('association', () => {
1814
1819
@Column
1815
1820
country : string ;
1816
1821
1817
- @ForeignKey ( ( ) => User4 )
1818
- @Column ( { field : 'user_id' } )
1819
- userId : number ;
1820
-
1821
1822
@BelongsTo ( ( ) => User4 , {
1822
1823
foreignKey : { allowNull : false , name : 'user_id' } ,
1823
1824
onDelete : 'CASCADE' ,
@@ -1861,6 +1862,48 @@ describe('association', () => {
1861
1862
1862
1863
oneToOneTestSuites ( User5 , Address5 ) ;
1863
1864
} ) ;
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
+ } ) ;
1864
1907
} ) ;
1865
1908
1866
1909
} ) ;
0 commit comments