Skip to content

Commit

Permalink
fix: fix one-to-one display for one side (#394)
Browse files Browse the repository at this point in the history
* fix: fix one-to-one display for one side

* changeset
  • Loading branch information
foyarash authored Sep 5, 2024
1 parent b013f25 commit d16560a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-eggs-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@premieroctet/next-admin": minor
---

fix: fix one-to-one display for one side (#393)
22 changes: 20 additions & 2 deletions apps/example/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ export const options: NextAdminOptions = {
format: "CSV",
url: "/api/users/export",
},
display: ["id", "name", "email", "posts", "role", "birthDate"],
display: [
"id",
"name",
"email",
"posts",
"role",
"birthDate",
"profile",
],
search: ["name", "email", "role"],
copy: ["email"],
filters: [
Expand Down Expand Up @@ -212,6 +220,16 @@ export const options: NextAdminOptions = {
},
},
},
Profile: {
title: "Profiles",
icon: "UserIcon",
list: {
display: ["id", "user"],
},
edit: {
display: ["user", "bio"],
},
},
},
pages: {
"/custom": {
Expand All @@ -223,7 +241,7 @@ export const options: NextAdminOptions = {
groups: [
{
title: "Users",
models: ["User"],
models: ["User", "Profile"],
},
{
title: "Categories",
Expand Down
10 changes: 8 additions & 2 deletions packages/next-admin/src/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,15 @@ export const findRelationInData = (
const dmmfPropertyRelationToFields = dmmfProperty.relationToFields;

if (dmmfPropertyKind === "object") {
/**
* Handle one-to-one relation
* Make sure that we are in a relation that is not a list
* because one side of a one-to-one relation will not have relationFromFields
*/
if (
dmmfPropertyRelationFromFields!.length > 0 &&
dmmfPropertyRelationToFields!.length > 0
(dmmfPropertyRelationFromFields!.length > 0 &&
dmmfPropertyRelationToFields!.length > 0) ||
!dmmfProperty.isList
) {
const idProperty = getModelIdProperty(dmmfProperty.type as ModelName);
data.forEach((item) => {
Expand Down

0 comments on commit d16560a

Please sign in to comment.