Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
node-version: '20.x'

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

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

- name: Check dependencies cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-deps-${{ hashFiles('**/package.json') }}-${{ hashFiles('**/package-lock.json') }}
Expand Down
14 changes: 7 additions & 7 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
node-version: '20.x'

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

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

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

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

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

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

- name: Check dependencies cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-deps-${{ hashFiles('**/package.json') }}-${{ hashFiles('**/package-lock.json') }}
Expand Down
50 changes: 37 additions & 13 deletions src/ui/components/ModelForm/AssociationFieldset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,35 +98,37 @@ function AssociationFieldset({
return

case AssociationTypeType.ManyToMany: {
const table_name = defaultThroughTableName(model.name, targetModel.name)
const table_name = defaultThroughTableName(model, targetModel, schema)
handleChange({ type: manyToManyTableType(table_name) })
return
}
}
},
[model.name, targetModel?.name, handleChange],
[model, targetModel, schema, handleChange],
)

const handleChangeTarget = React.useCallback(
(newTargetModel: Model) => {
// if current through table name is default, update table name to match new target
if (
isManytoMany(association) &&
isThroughTable(association.type.through) &&
snakeCase(association.type.through.table) ==
defaultThroughTableName(model.name, targetModel.name)
(snakeCase(association.type.through.table) ==
throughTableName(model.name, targetModel.name) ||
snakeCase(association.type.through.table) ==
throughTableName(targetModel.name, model.name))
) {
const tableName = defaultThroughTableName(model, newTargetModel, schema)

handleChange({
targetModelId: newTargetModel.id,
type: {
...association.type,
through: throughTable(defaultThroughTableName(model.name, newTargetModel.name)),
},
type: { ...association.type, through: throughTable(tableName) },
})
} else {
handleChange({ targetModelId: newTargetModel.id })
}
},
[handleChange, association, targetModel, model],
[handleChange, association, targetModel, model, schema],
)

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

const table = defaultThroughTableName(model.name, targetModel.name)

if (type === ThroughType.ThroughTable) {
const table = defaultThroughTableName(model, targetModel, schema)

handleChangeManyToMany({ through: throughTable(table) })
return
}
const table = throughTableName(model.name, targetModel.name)

const throughModel =
schema.models.find((m) => snakeCase(m.name) === table) || schema.models[0]
Expand All @@ -157,7 +160,7 @@ function AssociationFieldset({
handleChangeManyToMany({ through: buildThroughModel(throughModel.id) })
}
},
[association, model.name, targetModel, schema.models, handleChangeManyToMany],
[association, model, targetModel, schema, handleChangeManyToMany],
)

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

function defaultThroughTableName(modelName: string, targetModelName: string): string {
function throughTableName(modelName: string, targetModelName: string): string {
return snakeCase(`${modelName} ${targetModelName}`)
}

function defaultThroughTableName(sourceModel: Model, targetModel: Model, schema: Schema): string {
const inverseThroughTable = schema.models.reduce<string | undefined>(
(acc, m) =>
acc ||
m.associations.reduce<string | undefined>(
(acc, assoc) =>
!acc &&
assoc.targetModelId === sourceModel.id &&
assoc.sourceModelId == targetModel.id &&
assoc.type.type == AssociationTypeType.ManyToMany &&
assoc.type.through.type === ThroughType.ThroughTable
? assoc.type.through.table
: acc,
undefined,
),
undefined,
)

return inverseThroughTable || throughTableName(sourceModel.name, targetModel.name)
}

export function associationTypeId(association: Association): string {
return `association-type-${association.id}`
}
Expand Down
Loading