paljs / prisma-tools

Prisma tools to help you generate CRUD system for GraphQL servers
https://paljs.com
MIT License
684 stars 54 forks source link

Schema Error - The directive `@map` can not be used on relation fields #121

Closed hriziya closed 4 years ago

hriziya commented 4 years ago

Need help.

I am using the pal c command to create the apollo-nexus-schema and then I generated my prisma.schema using the yarn prisma introspect command and then I have used command pal schema camel-case from here and then run the command

yarn generate

It show me output like this with an error for every foreign key field I have.

yarn run v1.22.4 $ npm -s run generate:prisma && npm -s run generate:nexus Environment variables loaded from prisma\.env Error: Schema parsing error: Error parsing attribute "@map": The directive@mapcan not be used on relation fields. --> schema.prisma:33

schema.prisma Line no 33 looks like this. medicineType MedicineType @relation(fields: [medicineTypeId], references: [id]) @map("medicine_type")

I am not sure if this is an error from Prisma itself or Paljs.

Thank you

AhmedElywa commented 4 years ago

@hriziya this issue from our camel-case converter can you please share just the model have this issue before using our cli and after using it

hriziya commented 4 years ago

Before

model medicine {
  clinic_id        Int
  company          String?
  composition      String?
  created_at       DateTime           @default(now())
  enabled          Int                @default(1)
  id               Int                @default(autoincrement()) @id
  medicine_type_id Int
  name             String
  notes            String
  updated_at       DateTime           @default(now())
  clinic           clinic             @relation(fields: [clinic_id], references: [id])
  medicine_type    medicine_type      @relation(fields: [medicine_type_id], references: [id])
  opd_prescription opd_prescription[]

  @@index([medicine_type_id], name: "medicine_ibfk_1")
  @@index([clinic_id], name: "medicine_ibfk_2")
}

model medicine_type {
  enabled  Int        @default(1)
  id       Int        @default(autoincrement()) @id
  name     String
  medicine medicine[]
}

After


model medicine {
  clinicId        Int @map("clinic_id")
  company          String?
  composition      String?
  createdAt       DateTime           @default(now()) @map("created_at")
  enabled          Int                @default(1)
  id               Int                @default(autoincrement()) @id
  medicineTypeId Int @map("medicineType_id")
  name             String
  notes            String
  updatedAt       DateTime           @default(now()) @map("updated_at")
  clinic           clinic             @relation(fields: [clinicId], references: [id])
  medicineType    MedicineType      @relation(fields: [medicineTypeId], references: [id]) @map("medicine_type")
  opdPrescription OpdPrescription[] @map("opd_prescription")
  @@index([medicineTypeId], name: "medicine_ibfk_1")
  @@index([clinicId], name: "medicine_ibfk_2")
}

model MedicineType {
@@map("medicine_type")
  enabled  Int        @default(1)
  id       Int        @default(autoincrement()) @id
  name     String
  medicine medicine[]
}
AhmedElywa commented 4 years ago

@hriziya can you also send SQL code for medicine table

hriziya commented 4 years ago

Sure. here it is

CREATE TABLE `medicine` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(300) COLLATE utf8mb4_unicode_ci NOT NULL,
  `composition` varchar(300) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `company` varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `notes` varchar(500) COLLATE utf8mb4_unicode_ci NOT NULL,
  `medicine_type_id` int(11) NOT NULL,
  `clinic_id` int(11) NOT NULL,
  `enabled` int(11) NOT NULL DEFAULT 1,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp(),
  PRIMARY KEY (`id`),
  KEY `medicine_ibfk_2` (`clinic_id`),
  KEY `medicine_ibfk_1` (`medicine_type_id`),
  CONSTRAINT `medicine_ibfk_1` FOREIGN KEY (`clinic_id`) REFERENCES `clinic` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `medicine_ibfk_2` FOREIGN KEY (`medicine_type_id`) REFERENCES `medicine_type` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
AhmedElywa commented 4 years ago

Ok my cli is working perfectly and convert your names to camel case but Prisma did not support @map for relation fields so you need to update medicineType MedicineType @relation(fields: [medicineTypeId], references: [id]) @map("medicine_type") and make it medicine_type MedicineType @relation(fields: [medicineTypeId], references: [id])

hriziya commented 4 years ago

Thanks - That's what I understood. I posted here, to confirm I understand it correctly. I will give this solution a try and let you know. Thank you @AhmedElywa