@@ -16,6 +16,7 @@ const {
1616 getNamePrefixedWithSchemaName,
1717 getViewData,
1818 getDbVersion,
19+ addCommaPrefix,
1920} = require ( '../utils/general' ) ;
2021const assignTemplates = require ( '../utils/assignTemplates' ) ;
2122const {
@@ -137,19 +138,29 @@ module.exports = (baseProvider, options, app) => {
137138 const constraintWarnings = getConstraintsWarnings (
138139 keyConstraints . filter ( ( { errorMessage } ) => errorMessage ) ,
139140 ) ;
140- const keyConstraintsString = `${ generateConstraintsString (
141- dividedKeysConstraints ,
142- isActivated ,
143- ) } ${ constraintWarnings } `;
141+ const doKeyConstraintsHaveWarnings = Boolean ( constraintWarnings ) ;
142+ const keyConstraintsString = `${ generateConstraintsString ( {
143+ dividedConstraints : dividedKeysConstraints ,
144+ isParentActivated : isActivated ,
145+ } ) } ${ addCommaPrefix ( constraintWarnings , doKeyConstraintsHaveWarnings ) } `;
144146 const keyConstraintsValue = partitionOf ? keyConstraintsString ?. slice ( 1 ) : keyConstraintsString ;
145147
146148 const dividedForeignKeys = divideIntoActivatedAndDeactivated ( foreignKeyConstraints , key => key . statement ) ;
147- const foreignKeyConstraintsString = generateConstraintsString ( dividedForeignKeys , isActivated ) ;
149+ const shouldAddCommaPrefixToForeignKeysConstraints =
150+ ! doKeyConstraintsHaveWarnings || checkConstraints . length !== 0 ;
151+ const foreignKeyConstraintsString = generateConstraintsString ( {
152+ dividedConstraints : dividedForeignKeys ,
153+ isParentActivated : isActivated ,
154+ activatedConstraintsPrefix : addCommaPrefix ( '\n\t' , shouldAddCommaPrefixToForeignKeysConstraints ) ,
155+ } ) ;
148156
149157 const columnDescriptions = '\n' + getColumnComments ( tableName , columnDefinitions ) ;
150158 const template = partitionOf ? templates . createTablePartitionOf : templates . createTable ;
151159
152- const checkConstraintPrefix = partitionOf && ! keyConstraintsString ? '\n\t' : ',\n\t' ;
160+ const checkConstraintPrefix =
161+ partitionOf && ! keyConstraintsString
162+ ? '\n\t'
163+ : `${ addCommaPrefix ( '\n\t' , ! doKeyConstraintsHaveWarnings ) } ` ;
153164 const checkConstraintsValue = ! _ . isEmpty ( checkConstraints )
154165 ? wrap ( _ . join ( checkConstraints , ',\n\t' ) , checkConstraintPrefix , '' )
155166 : '' ;
0 commit comments