Repository: Quramy/prisma-fabbrica (ts)

Issue:
Cannot convert when composite unique key and Primary Key are not set
**Describe the bug**
https://github.com/Quramy/prisma-fabbrica/issues/55

The above issue you responded to.
I am still experiencing the same problem with the schema.prisma described in the issue.
Sorry for the lack of explanation.

対応いただいた上記issueですが、
issueに記載したschema.prismaで現在も同様の現象が発生しています。
説明不足で申し訳ございません。

```sh
$ npx prisma generate
Prisma schema loaded from prisma/schema.prisma
Error: 
✔ Generated Prisma Client (4.6.1 | library) to ./../../node_modules/@prisma/client in 58ms

Cannot read properties of undefined (reading 'name')
```

**To Reproduce**
Define composite unique key with @@unique
&&
Occurs when Primary Key does not exist for a model.
If @@id and @@id are added, it will not occur.

@@uniqueで複合ユニークキーを定義
&&
Primary Keyが存在しないmodelの場合に発生。
@id, @@idを追加すれば発生しない。

**Expected behavior**
Successful fabbrica conversion

正常にfabbricaの変換が行われる

**Your Schema**

```
// Paste schema.prisma file.
model Company {
  id    String @id
  name  String
  companyEmployees CompanyEmployee[]
}

model Employee {
  id        String   @id
  name      String
  companyEmployees CompanyEmployee?
}

model CompanyEmployee {
  company   Company  @relation(fields: [companyId], references: [id])
  companyId String
  employee  Employee @relation(fields: [employeeId], references: [id])
  employeeId String @unique

  @@unique([companyId, employeeId])
}
```

**Environment (please complete the following information):**

- Database kind [sqlite]
- Node.js version [18.11.0]
- @prisma/client version [4.6.1]
- TypeScript version [4.9.3]

**Additional context**
Add any other context about the problem here.

Please put gist URL including generated .js and .d.ts if you can :pray:


Pull request description:
fix: Extract unique fields when @@unique
It fixes #57 .

Patch:
diff --git a/packages/prisma-fabbrica/src/templates/index.ts b/packages/prisma-fabbrica/src/templates/index.ts
index c155332..449909b 100644
--- a/packages/prisma-fabbrica/src/templates/index.ts
+++ b/packages/prisma-fabbrica/src/templates/index.ts
@@ -12,7 +12,17 @@ export function findPrsimaCreateInputTypeFromModelName(document: DMMF.Document,
 }
 
 export function getIdFieldNames(model: DMMF.Model) {
-  return model.primaryKey?.fields ?? [model.fields.find(f => f.isId || f.isUnique)!.name];
+  if (model.primaryKey) {
+    return model.primaryKey.fields;
+  }
+  const idLike = model.fields.find(f => f.isId || f.isUnique);
+  if (idLike) {
+    return [idLike.name];
+  }
+  if (model.uniqueFields.length) {
+    return model.uniqueFields[0];
+  }
+  throw new Error(`Model ${model.name} does not have @id nor @@id nor @@unique.`);
 }
 
 function filterRequiredFields(inputType: DMMF.InputType) {


Test patch:
diff --git a/packages/prisma-fabbrica/src/templates/getIdFieldNames.test.ts b/packages/prisma-fabbrica/src/templates/getIdFieldNames.test.ts
index c43e798..3d4b960 100644
--- a/packages/prisma-fabbrica/src/templates/getIdFieldNames.test.ts
+++ b/packages/prisma-fabbrica/src/templates/getIdFieldNames.test.ts
@@ -34,6 +34,17 @@ describe(modelScalarOrEnumFields, () => {
       `,
       expected: ["field1", "field2"],
     },
+    {
+      pattern: "Complex unique key",
+      datamodel: `
+        model TestModel {
+          field1 Int
+          field2 Int
+          @@unique([field1, field2])
+        }
+      `,
+      expected: ["field1", "field2"],
+    },
   ])("generates literal type field for $pattern", async ({ datamodel, expected }) => {
     const dmmf = await getDMMF({
       datamodel,
diff --git a/packages/ts-compile-testing/fixtures/field-variation/__generated__/fabbrica/index.ts b/packages/ts-compile-testing/fixtures/field-variation/__generated__/fabbrica/index.ts
index 6fad97c..8d6d8cd 100644
--- a/packages/ts-compile-testing/fixtures/field-variation/__generated__/fabbrica/index.ts
+++ b/packages/ts-compile-testing/fixtures/field-variation/__generated__/fabbrica/index.ts
@@ -168,8 +168,10 @@ type FieldTypePatternModelFactoryDefineInput = {
     id?: number;
     requiredInt?: number;
     nullableInt?: number | null;
+    enumerableInt?: Prisma.FieldTypePatternModelCreateenumerableIntInput | Prisma.Enumerable<number>;
     requiredStr?: string;
     nullableStr?: string | null;
+    enumerableStr?: Prisma.FieldTypePatternModelCreateenumerableStrInput | Prisma.Enumerable<string>;
     requiredBool?: boolean;
     nullableBool?: boolean | null;
     requiredFlaot?: number;
diff --git a/packages/ts-compile-testing/fixtures/field-variation/schema.prisma b/packages/ts-compile-testing/fixtures/field-variation/schema.prisma
index a7d2df9..6863d19 100644
--- a/packages/ts-compile-testing/fixtures/field-variation/schema.prisma
+++ b/packages/ts-compile-testing/fixtures/field-variation/schema.prisma
@@ -36,8 +36,10 @@ model FieldTypePatternModel {
   id              Int @id
   requiredInt     Int
   nullableInt     Int?
+  enumerableInt   Int[]
   requiredStr     String
   nullableStr     String?
+  enumerableStr   String[]
   requiredBool    Boolean
   nullableBool    Boolean?
   requiredFlaot   Float