@@ -151,26 +151,13 @@ export function syncTable({
151151
152152 const modelFactory = new DataModelFactory ( ) . setName ( name ) . setIsView ( table . type === 'view' ) ;
153153 modelFactory . setContainer ( model ) ;
154+
154155 if ( modified ) {
155156 modelFactory . addAttribute ( ( builder ) =>
156157 builder . setDecl ( tableMapAttribute ) . addArg ( ( argBuilder ) => argBuilder . StringLiteral . setValue ( table . name ) ) ,
157158 ) ;
158159 }
159160
160- if ( multiPk ) {
161- const pkColumns = table . columns . filter ( ( c ) => c . pk ) . map ( ( c ) => c . name ) ;
162- modelFactory . addAttribute ( ( builder ) =>
163- builder . setDecl ( modelIdAttribute ) . addArg ( ( argBuilder ) => {
164- const arrayExpr = argBuilder . ArrayExpr ;
165- pkColumns . map ( ( c ) => {
166- const ref = modelFactory . node . fields . find ( ( f ) => getDbName ( f ) === c ) ! ;
167- arrayExpr . addItem ( ( itemBuilder ) => itemBuilder . ReferenceExpr . setTarget ( ref ) ) ;
168- } ) ;
169- return arrayExpr ;
170- } ) ,
171- ) ;
172- }
173-
174161 table . columns . forEach ( ( column ) => {
175162 if ( column . foreign_key_table ) {
176163 relations . push ( {
@@ -231,7 +218,7 @@ export function syncTable({
231218 enums : model . declarations . filter ( ( d ) => d . $type === 'Enum' ) as Enum [ ] ,
232219 } )
233220 : [ ] ;
234- defaultValuesAttrs . forEach ( builder . addAttribute ) ;
221+ defaultValuesAttrs . forEach ( builder . addAttribute . bind ( builder ) ) ;
235222 }
236223
237224 if ( column . pk && ! multiPk ) {
@@ -254,12 +241,12 @@ export function syncTable({
254241 } ) ;
255242 } ) ;
256243
257- const uniqieColumns = table . columns . filter ( ( c ) => c . unique && ! c . pk ) . map ( ( c ) => c . name ) ;
258- if ( uniqieColumns . length > 0 ) {
244+ const pkColumns = table . columns . filter ( ( c ) => c . pk ) . map ( ( c ) => c . name ) ;
245+ if ( multiPk ) {
259246 modelFactory . addAttribute ( ( builder ) =>
260- builder . setDecl ( modelUniqueAttribute ) . addArg ( ( argBuilder ) => {
247+ builder . setDecl ( modelIdAttribute ) . addArg ( ( argBuilder ) => {
261248 const arrayExpr = argBuilder . ArrayExpr ;
262- uniqieColumns . map ( ( c ) => {
249+ pkColumns . map ( ( c ) => {
263250 const ref = modelFactory . node . fields . find ( ( f ) => getDbName ( f ) === c ) ! ;
264251 arrayExpr . addItem ( ( itemBuilder ) => itemBuilder . ReferenceExpr . setTarget ( ref ) ) ;
265252 } ) ;
@@ -268,21 +255,65 @@ export function syncTable({
268255 ) ;
269256 }
270257
271- model . declarations . push ( modelFactory . node ) ;
272-
273- table . indexes . forEach ( ( index ) => {
258+ const uniqieColumns = table . columns . filter ( ( c ) => c . unique && ! c . pk ) . map ( ( c ) => c . name ) ;
259+ if ( uniqieColumns . length > 0 ) {
274260 modelFactory . addAttribute ( ( builder ) =>
275- builder . setDecl ( modelindexAttribute ) . addArg ( ( argBuilder ) => {
261+ builder . setDecl ( modelUniqueAttribute ) . addArg ( ( argBuilder ) => {
276262 const arrayExpr = argBuilder . ArrayExpr ;
277- index . columns . map ( ( c ) => {
278- const ref = modelFactory . node . fields . find ( ( f ) => getDbName ( f ) === c . name ) ! ;
263+ uniqieColumns . map ( ( c ) => {
264+ const ref = modelFactory . node . fields . find ( ( f ) => getDbName ( f ) === c ) ! ;
279265 arrayExpr . addItem ( ( itemBuilder ) => itemBuilder . ReferenceExpr . setTarget ( ref ) ) ;
280266 } ) ;
281267 return arrayExpr ;
282268 } ) ,
283269 ) ;
270+ }
271+
272+ table . indexes . forEach ( ( index ) => {
273+ if ( index . predicate ) {
274+ //These constraints are not supported by Zenstack, because Zenstack currently does not fully support check constraints. Read more: https://pris.ly/d/check-constraints
275+ console . log (
276+ 'These constraints are not supported by Zenstack. Read more: https://pris.ly/d/check-constraints' ,
277+ `- Model: "${ table . name } ", constraint: "${ index . name } "` ,
278+ ) ;
279+ return ;
280+ }
281+ if ( index . columns . find ( ( c ) => c . expression ) ) {
282+ console . log (
283+ 'These constraints are not supported by Zenstack. Read more: https://pris.ly/d/check-constraints' ,
284+ `- Model: "${ table . name } ", constraint: "${ index . name } "` ,
285+ ) ;
286+ return ;
287+ }
288+
289+ if ( index . columns . length === 1 && index . columns . find ( ( c ) => pkColumns . includes ( c . name ) ) ) {
290+ //skip primary key
291+ return ;
292+ }
293+
294+ modelFactory . addAttribute ( ( builder ) =>
295+ builder
296+ . setDecl ( index . unique ? modelUniqueAttribute : modelindexAttribute )
297+ . addArg ( ( argBuilder ) => {
298+ const arrayExpr = argBuilder . ArrayExpr ;
299+ index . columns . map ( ( c ) => {
300+ const ref = modelFactory . node . fields . find ( ( f ) => getDbName ( f ) === c . name ) ! ;
301+ if ( ! ref ) console . log ( c , table . name ) ;
302+ arrayExpr . addItem ( ( itemBuilder ) => {
303+ const refExpr = itemBuilder . ReferenceExpr . setTarget ( ref ) ;
304+ if ( c . order !== 'ASC' ) refExpr . addArg ( ( ab ) => ab . StringLiteral . setValue ( 'DESC' ) , 'sort' ) ;
305+
306+ return refExpr ;
307+ } ) ;
308+ } ) ;
309+ return arrayExpr ;
310+ } )
311+ . addArg ( ( argBuilder ) => argBuilder . StringLiteral . setValue ( index . name ) , 'map' ) ,
312+ ) ;
284313 } ) ;
285314
315+ model . declarations . push ( modelFactory . node ) ;
316+
286317 return relations ;
287318}
288319
@@ -327,12 +358,15 @@ export function syncRelation({
327358 const fieldPrefix = / [ 0 - 9 ] / g. test ( sourceModel . name . charAt ( 0 ) ) ? '_' : '' ;
328359
329360 const relationName = `${ sourceModel . name } _${ relation . column } To${ targetModel . name } _${ relation . references . column } ` ;
361+ let sourceFieldName = `${ fieldPrefix } ${ sourceModel . name . charAt ( 0 ) . toLowerCase ( ) } ${ sourceModel . name . slice ( 1 ) } _${ relation . column } ` ;
362+
363+ if ( sourceModel . fields . find ( ( f ) => f . name === sourceFieldName ) ) {
364+ sourceFieldName = `${ sourceFieldName } To${ targetModel . name . charAt ( 0 ) . toLowerCase ( ) } ${ targetModel . name . slice ( 1 ) } _${ relation . references . column } ` ;
365+ }
330366
331367 const sourceFieldFactory = new DataFieldFactory ( )
332368 . setContainer ( sourceModel )
333- . setName (
334- `${ fieldPrefix } ${ sourceModel . name . charAt ( 0 ) . toLowerCase ( ) } ${ sourceModel . name . slice ( 1 ) } _${ relation . column } ` ,
335- )
369+ . setName ( sourceFieldName )
336370 . setType ( ( tb ) =>
337371 tb
338372 . setOptional ( relation . nullable )
@@ -345,7 +379,7 @@ export function syncRelation({
345379 . addArg ( ( ab ) => ab . StringLiteral . setValue ( relationName ) )
346380 . addArg ( ( ab ) => ab . ArrayExpr . addItem ( ( aeb ) => aeb . ReferenceExpr . setTarget ( sourceField ) ) , 'fields' )
347381 . addArg ( ( ab ) => ab . ArrayExpr . addItem ( ( aeb ) => aeb . ReferenceExpr . setTarget ( targetField ) ) , 'references' )
348- . addArg ( ( ab ) => ab . ArrayExpr . addItem ( ( aeb ) => aeb . StringLiteral . setValue ( relation . fk_name ) ) , 'map' ) ,
382+ . addArg ( ( ab ) => ab . StringLiteral . setValue ( relation . fk_name ) , 'map' ) ,
349383 ) ;
350384
351385 sourceModel . fields . push ( sourceFieldFactory . node ) ;
0 commit comments