Skip to content

Commit

Permalink
handle if references already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Druue committed Jul 10, 2024
1 parent 5bf1d05 commit 9470875
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
6 changes: 5 additions & 1 deletion prisma-fmt/src/code_actions/relations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,11 @@ pub(super) fn add_referencing_side_relation(
.find(|arg| arg.value.is_string())
.map_or(Default::default(), |arg| format!("{arg}, "));

Check warning on line 284 in prisma-fmt/src/code_actions/relations.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/prisma-engines/prisma-engines/prisma-fmt/src/code_actions/relations.rs
let relation_attr = format!("@relation({}fields: [], references: [])", name);
let references = initiating_field
.referenced_fields()
.map_or("references: []".to_owned(), |refs| format!("references: [{}]", refs.map(|r| r.name()).collect::<Vec<&str>>().join(", ")));

let relation_attr = format!("@relation({}fields: [], {})", name, references);
let range = super::span_to_range(attr.span(), ctx.initiating_file_source());

TextEdit {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[
{
"title": "Add relation attribute for relation field",
"kind": "quickfix",
"diagnostics": [
{
"range": {
"start": {
"line": 3,
"character": 4
},
"end": {
"line": 4,
"character": 0
}
},
"severity": 1,
"message": "Error parsing attribute \"@relation\": The relation field `organization` on Model `user` must specify the `fields` argument in the @relation attribute. You can run `prisma format` to fix this automatically."
}
],
"edit": {
"changes": {
"file:///path/to/schema.prisma": [
{
"range": {
"start": {
"line": 3,
"character": 33
},
"end": {
"line": 3,
"character": 60
}
},
"newText": "@relation(fields: [], references: [id])"
}
]
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
model user {
id String @id
email String @unique
organization organization? @relation(references: [id])
}

model organization {
id String @id
users user[]
}
1 change: 1 addition & 0 deletions prisma-fmt/tests/code_actions/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ scenarios! {
add_missing_relation_attribute_mongodb
add_missing_relation_attribute_args
add_missing_relation_attribute_args_self
add_missing_relation_attribute_args_references
one_to_many_referenced_side_misses_unique_single_field
one_to_many_referenced_side_misses_unique_single_field_multifile
one_to_many_referenced_side_misses_unique_single_field_broken_relation
Expand Down

0 comments on commit 9470875

Please sign in to comment.