diff --git a/.changeset/chilled-eggs-brush.md b/.changeset/chilled-eggs-brush.md new file mode 100644 index 00000000..abcae68f --- /dev/null +++ b/.changeset/chilled-eggs-brush.md @@ -0,0 +1,5 @@ +--- +"@premieroctet/next-admin": minor +--- + +fix: fix one-to-one display for one side (#393) diff --git a/apps/example/options.tsx b/apps/example/options.tsx index a9e0f2ea..a09cd579 100644 --- a/apps/example/options.tsx +++ b/apps/example/options.tsx @@ -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: [ @@ -212,6 +220,16 @@ export const options: NextAdminOptions = { }, }, }, + Profile: { + title: "Profiles", + icon: "UserIcon", + list: { + display: ["id", "user"], + }, + edit: { + display: ["user", "bio"], + }, + }, }, pages: { "/custom": { @@ -223,7 +241,7 @@ export const options: NextAdminOptions = { groups: [ { title: "Users", - models: ["User"], + models: ["User", "Profile"], }, { title: "Categories", diff --git a/packages/next-admin/src/utils/server.ts b/packages/next-admin/src/utils/server.ts index 29457f80..8ea27ea8 100644 --- a/packages/next-admin/src/utils/server.ts +++ b/packages/next-admin/src/utils/server.ts @@ -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) => {