Skip to content

Commit bc4b7fe

Browse files
authored
Default to existing through table if inverse m2m assoc exists (#130)
* Default to existing through table if inverse m2m assoc exists * update cache action versions
1 parent 3df3b43 commit bc4b7fe

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
node-version: '20.x'
2121

2222
- name: Check dependencies cache
23-
uses: actions/cache@v2
23+
uses: actions/cache@v4
2424
with:
2525
path: node_modules
2626
key: ${{ runner.os }}-deps-${{ hashFiles('**/package.json') }}-${{ hashFiles('**/package-lock.json') }}
@@ -47,7 +47,7 @@ jobs:
4747
node-version: '20.x'
4848

4949
- name: Check dependencies cache
50-
uses: actions/cache@v2
50+
uses: actions/cache@v4
5151
with:
5252
path: node_modules
5353
key: ${{ runner.os }}-deps-${{ hashFiles('**/package.json') }}-${{ hashFiles('**/package-lock.json') }}
@@ -74,7 +74,7 @@ jobs:
7474
node-version: '20.x'
7575

7676
- name: Check dependencies cache
77-
uses: actions/cache@v2
77+
uses: actions/cache@v4
7878
with:
7979
path: node_modules
8080
key: ${{ runner.os }}-deps-${{ hashFiles('**/package.json') }}-${{ hashFiles('**/package-lock.json') }}

.github/workflows/production.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
node-version: '20.x'
2222

2323
- name: Check dependencies cache
24-
uses: actions/cache@v2
24+
uses: actions/cache@v4
2525
with:
2626
path: node_modules
2727
key: ${{ runner.os }}-deps-${{ hashFiles('**/package.json') }}-${{ hashFiles('**/package-lock.json') }}
@@ -48,7 +48,7 @@ jobs:
4848
node-version: '20.x'
4949

5050
- name: Check dependencies cache
51-
uses: actions/cache@v2
51+
uses: actions/cache@v4
5252
with:
5353
path: node_modules
5454
key: ${{ runner.os }}-deps-${{ hashFiles('**/package.json') }}-${{ hashFiles('**/package-lock.json') }}
@@ -75,7 +75,7 @@ jobs:
7575
node-version: '20.x'
7676

7777
- name: Check dependencies cache
78-
uses: actions/cache@v2
78+
uses: actions/cache@v4
7979
with:
8080
path: node_modules
8181
key: ${{ runner.os }}-deps-${{ hashFiles('**/package.json') }}-${{ hashFiles('**/package-lock.json') }}
@@ -110,7 +110,7 @@ jobs:
110110
node-version: '20.x'
111111

112112
- name: Check dependencies cache
113-
uses: actions/cache@v2
113+
uses: actions/cache@v4
114114
with:
115115
path: node_modules
116116
key: ${{ runner.os }}-deps-${{ hashFiles('**/package.json') }}-${{ hashFiles('**/package-lock.json') }}
@@ -142,7 +142,7 @@ jobs:
142142
node-version: '20.x'
143143

144144
- name: Check dependencies cache
145-
uses: actions/cache@v2
145+
uses: actions/cache@v4
146146
with:
147147
path: node_modules
148148
key: ${{ runner.os }}-deps-${{ hashFiles('**/package.json') }}-${{ hashFiles('**/package-lock.json') }}
@@ -174,7 +174,7 @@ jobs:
174174
node-version: '20.x'
175175

176176
- name: Check dependencies cache
177-
uses: actions/cache@v2
177+
uses: actions/cache@v4
178178
with:
179179
path: node_modules
180180
key: ${{ runner.os }}-deps-${{ hashFiles('**/package.json') }}-${{ hashFiles('**/package-lock.json') }}
@@ -207,7 +207,7 @@ jobs:
207207
node-version: '20.x'
208208

209209
- name: Check dependencies cache
210-
uses: actions/cache@v2
210+
uses: actions/cache@v4
211211
with:
212212
path: node_modules
213213
key: ${{ runner.os }}-deps-${{ hashFiles('**/package.json') }}-${{ hashFiles('**/package-lock.json') }}

src/ui/components/ModelForm/AssociationFieldset.tsx

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,35 +98,37 @@ function AssociationFieldset({
9898
return
9999

100100
case AssociationTypeType.ManyToMany: {
101-
const table_name = defaultThroughTableName(model.name, targetModel.name)
101+
const table_name = defaultThroughTableName(model, targetModel, schema)
102102
handleChange({ type: manyToManyTableType(table_name) })
103103
return
104104
}
105105
}
106106
},
107-
[model.name, targetModel?.name, handleChange],
107+
[model, targetModel, schema, handleChange],
108108
)
109109

110110
const handleChangeTarget = React.useCallback(
111111
(newTargetModel: Model) => {
112+
// if current through table name is default, update table name to match new target
112113
if (
113114
isManytoMany(association) &&
114115
isThroughTable(association.type.through) &&
115-
snakeCase(association.type.through.table) ==
116-
defaultThroughTableName(model.name, targetModel.name)
116+
(snakeCase(association.type.through.table) ==
117+
throughTableName(model.name, targetModel.name) ||
118+
snakeCase(association.type.through.table) ==
119+
throughTableName(targetModel.name, model.name))
117120
) {
121+
const tableName = defaultThroughTableName(model, newTargetModel, schema)
122+
118123
handleChange({
119124
targetModelId: newTargetModel.id,
120-
type: {
121-
...association.type,
122-
through: throughTable(defaultThroughTableName(model.name, newTargetModel.name)),
123-
},
125+
type: { ...association.type, through: throughTable(tableName) },
124126
})
125127
} else {
126128
handleChange({ targetModelId: newTargetModel.id })
127129
}
128130
},
129-
[handleChange, association, targetModel, model],
131+
[handleChange, association, targetModel, model, schema],
130132
)
131133

132134
const handleChangeAlias = React.useCallback(
@@ -143,12 +145,13 @@ function AssociationFieldset({
143145
(type: ThroughType) => {
144146
if (!isManytoMany(association)) return
145147

146-
const table = defaultThroughTableName(model.name, targetModel.name)
147-
148148
if (type === ThroughType.ThroughTable) {
149+
const table = defaultThroughTableName(model, targetModel, schema)
150+
149151
handleChangeManyToMany({ through: throughTable(table) })
150152
return
151153
}
154+
const table = throughTableName(model.name, targetModel.name)
152155

153156
const throughModel =
154157
schema.models.find((m) => snakeCase(m.name) === table) || schema.models[0]
@@ -157,7 +160,7 @@ function AssociationFieldset({
157160
handleChangeManyToMany({ through: buildThroughModel(throughModel.id) })
158161
}
159162
},
160-
[association, model.name, targetModel, schema.models, handleChangeManyToMany],
163+
[association, model, targetModel, schema, handleChangeManyToMany],
161164
)
162165

163166
const handleChangeThroughModel = React.useCallback(
@@ -286,10 +289,31 @@ function aliasPlaceholder(association: Association, model: Model): string | unde
286289
: plural(model.name)
287290
}
288291

289-
function defaultThroughTableName(modelName: string, targetModelName: string): string {
292+
function throughTableName(modelName: string, targetModelName: string): string {
290293
return snakeCase(`${modelName} ${targetModelName}`)
291294
}
292295

296+
function defaultThroughTableName(sourceModel: Model, targetModel: Model, schema: Schema): string {
297+
const inverseThroughTable = schema.models.reduce<string | undefined>(
298+
(acc, m) =>
299+
acc ||
300+
m.associations.reduce<string | undefined>(
301+
(acc, assoc) =>
302+
!acc &&
303+
assoc.targetModelId === sourceModel.id &&
304+
assoc.sourceModelId == targetModel.id &&
305+
assoc.type.type == AssociationTypeType.ManyToMany &&
306+
assoc.type.through.type === ThroughType.ThroughTable
307+
? assoc.type.through.table
308+
: acc,
309+
undefined,
310+
),
311+
undefined,
312+
)
313+
314+
return inverseThroughTable || throughTableName(sourceModel.name, targetModel.name)
315+
}
316+
293317
export function associationTypeId(association: Association): string {
294318
return `association-type-${association.id}`
295319
}

0 commit comments

Comments
 (0)