Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to use FK as id(as a composite key) #24218

Closed
duri0214 opened this issue May 16, 2024 · 3 comments
Closed

I want to use FK as id(as a composite key) #24218

duri0214 opened this issue May 16, 2024 · 3 comments

Comments

@duri0214
Copy link

duri0214 commented May 16, 2024

Problem

When I set a composite key that includes FK as an ID, the ID violates the unique key.
Even if you create a composite key, a unique constraint is created on the FK, so duplicate FKs are not allowed.
I think it is possible to have a composite key like this in business.

classDiagram
  class areas {
     id: INT
     name: string
  }
  class weather_forecast {
      date: Date
     temperature: INT
     area_id: FK
  }
  weather_forecast --> areas : areas_id
CREATE TABLE `weather_forecasts` (
  `areas_id` VARCHAR(191) NOT NULL,
  `date` VARCHAR(191) NOT NULL,
  `temperature` INTEGER NOT NULL,
  `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `updated_at` DATETIME(3) NULL,
  
  UNIQUE INDEX `weather_forecasts_areas_id_key` (`areas_id`),
  UNIQUE INDEX `weather_forecasts_areas_id_date_key` (`areas_id`, `date`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Suggested solution

Ability to create a composite key of date and xxx_id

Alternatives

Additional context

@duri0214 duri0214 changed the title I want to use FK as id I want to use FK as id(as a composite key) May 16, 2024
@janpio
Copy link
Member

janpio commented May 16, 2024

What is the Prisma context of your question? What Prisma schema do you get when you introspect your database that has that behavior?

(Aside: The SQL you are sharing does not show any foreign key, so does not match the ERD or the issue title)

@duri0214
Copy link
Author

duri0214 commented May 16, 2024

thank you for response :)
This is what it looks like when written in prisma schema.

model Areas {
  id String @id
  name String
  weatherForecasts WeatherForecasts[]
}
model WeatherForecasts{
  areas Areas @relation(fields: [areasId], references: [id], onDelete: Cascade)
  areasId String @unique @map("areas_id")
  date String
  temperature: Int
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime? @updateAt @map("updated_at")
}

@@unique([date, areasId])
@@map("weather_forecasts")

@janpio
Copy link
Member

janpio commented May 16, 2024

That is not a valid Prisma schema (for multiple reasons, we do not support : after the field name and no @@unique or @@map outside of models).

Can you please clarify your issue or question? Thanks.

@prisma prisma locked and limited conversation to collaborators May 17, 2024
@janpio janpio converted this issue into discussion #24229 May 17, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants