Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wren-ui): fix auto-generated relationships #263

Merged
merged 4 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 20 additions & 4 deletions wren-ui/src/apollo/server/services/modelService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ export class ModelService implements IModelService {
if (!toColumn) {
throw new Error(`Column not found, column Id ${relation.toColumnId}`);
}
const relationName = this.generateRelationName(relation, models);
const relationName = this.generateRelationName(relation, models, columns);
return {
projectId: project.id,
name: relationName,
Expand Down Expand Up @@ -385,7 +385,7 @@ export class ModelService implements IModelService {
if (!valid) {
throw new Error(message);
}
const relationName = this.generateRelationName(relation, models);
const relationName = this.generateRelationName(relation, models, columns);
const savedRelation = await this.relationRepository.createOne({
projectId: project.id,
name: relationName,
Expand Down Expand Up @@ -492,17 +492,33 @@ export class ModelService implements IModelService {
return replaceAllowableSyntax(displayName);
}

private generateRelationName(relation: RelationData, models: Model[]) {
private generateRelationName(
relation: RelationData,
models: Model[],
columns: ModelColumn[],
) {
const fromModel = models.find((m) => m.id === relation.fromModelId);
const toModel = models.find((m) => m.id === relation.toModelId);
if (!fromModel || !toModel) {
throw new Error('Model not found');
}

const fromColumn = columns.find(
(column) => column.id === relation.fromColumnId,
);
const toColumn = columns.find(
(column) => column.id === relation.toColumnId,
);

return (
fromModel.sourceTableName.charAt(0).toUpperCase() +
fromModel.sourceTableName.slice(1) +
fromColumn.referenceName.charAt(0).toUpperCase() +
fromColumn.referenceName.slice(1) +
toModel.sourceTableName.charAt(0).toUpperCase() +
toModel.sourceTableName.slice(1)
toModel.sourceTableName.slice(1) +
toColumn.referenceName.charAt(0).toUpperCase() +
toColumn.referenceName.slice(1)
);
fredalai marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
2 changes: 1 addition & 1 deletion wren-ui/src/components/pages/setup/DefineRelations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function EditableRelationTable(props: EditableRelationTableProps) {
</Button>
)}
rowKey={(record: RelationsDataType) =>
`${modelName}-${record.toField.modelName}-${index}`
`${modelName}-${record.fromField.fieldName}-${record.toField.modelName}-${record.toField.fieldName}-${index}`
}
/>
</div>
Expand Down