Quick fix for #3485 - default the key to the primary key but allow another field to be selected.

This commit is contained in:
mike12345567 2021-11-23 12:10:40 +00:00
parent f158ee8f57
commit 8be84e5602
1 changed files with 22 additions and 4 deletions

View File

@ -71,6 +71,9 @@
if ($touched.toCol && !toRelate.name) { if ($touched.toCol && !toRelate.name) {
errors.toCol = colNotSet errors.toCol = colNotSet
} }
if ($touched.primary && !fromPrimary) {
errors.primary = "Please pick the primary key"
}
// currently don't support relationships back onto the table itself, needs to relate out // currently don't support relationships back onto the table itself, needs to relate out
const tableError = "From/to/through tables must be different" const tableError = "From/to/through tables must be different"
if (fromTable && (fromTable === toTable || fromTable === throughTable)) { if (fromTable && (fromTable === toTable || fromTable === throughTable)) {
@ -95,6 +98,12 @@
return errors return errors
} }
$: isManyToMany =
fromRelationship?.relationshipType === RelationshipTypes.MANY_TO_MANY
$: isManyToOne =
fromRelationship?.relationshipType === RelationshipTypes.MANY_TO_ONE
$: fromPrimary =
!fromPrimary && fromTable ? fromTable.primary[0] : fromPrimary
$: tableOptions = plusTables.map(table => ({ $: tableOptions = plusTables.map(table => ({
label: table.name, label: table.name,
value: table._id, value: table._id,
@ -179,13 +188,13 @@
// foreignKey is what is linking out of the current table. // foreignKey is what is linking out of the current table.
relateFrom = { relateFrom = {
...relateFrom, ...relateFrom,
foreignKey: fromTable.primary[0], foreignKey: fromPrimary,
} }
relateTo = { relateTo = {
...relateTo, ...relateTo,
relationshipType: RelationshipTypes.ONE_TO_MANY, relationshipType: RelationshipTypes.ONE_TO_MANY,
foreignKey: relateFrom.fieldName, foreignKey: relateFrom.fieldName,
fieldName: fromTable.primary[0], fieldName: fromPrimary,
} }
} }
@ -264,6 +273,15 @@
bind:error={errors.from} bind:error={errors.from}
bind:value={toRelationship.tableId} bind:value={toRelationship.tableId}
/> />
{#if isManyToOne && fromTable}
<Select
label={`Primary Key (${fromTable?.name})`}
options={Object.keys(fromTable?.schema)}
on:change={() => ($touched.primary = true)}
bind:error={errors.primary}
bind:value={fromPrimary}
/>
{/if}
<Select <Select
label={"Select to table"} label={"Select to table"}
options={tableOptions} options={tableOptions}
@ -271,7 +289,7 @@
bind:error={errors.to} bind:error={errors.to}
bind:value={fromRelationship.tableId} bind:value={fromRelationship.tableId}
/> />
{#if fromRelationship?.relationshipType === RelationshipTypes.MANY_TO_MANY} {#if isManyToMany}
<Select <Select
label={"Through"} label={"Through"}
options={tableOptions} options={tableOptions}
@ -295,7 +313,7 @@
bind:value={fromRelationship.throughFrom} bind:value={fromRelationship.throughFrom}
/> />
{/if} {/if}
{:else if fromRelationship?.relationshipType && toTable} {:else if isManyToOne && toTable}
<Select <Select
label={`Foreign Key (${toTable?.name})`} label={`Foreign Key (${toTable?.name})`}
options={Object.keys(toTable?.schema).filter( options={Object.keys(toTable?.schema).filter(