question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Add a PK field to prisma generated mapping tables

See original GitHub issue

Hi,

I’m using a managed DB hosting provider (OVH) and in turn have no control over the sql_require_primary_key setting. When importing the prisma generated DB, I get an error saying each table must have a primary key. For the most part, this is fine but I’m struggling with the tables that prisma creates as mapping tables.

Take this model:

model User {
  id          Int      @id @default(autoincrement())
  canActAs    User[] @relation("actingAs", references: [id])
  hasActingAs User[] @relation("actingAs", references: [id])
}

Prisma will create a _actingAs table with 2 columns, A and B. I need to be able to add a PK field to that table.

Note: I tried creating an empty migration which altered those mapping tables adding a PK field but it created a new migration removing it after because the schema was out of line.

Note: Initially, I didn’t think those tables were causing my error because when I look at the _actingAs mapping table, I do see that both columns A and B are down as PK fields: image but the following query raises them as not having a PK field:

select tab.table_schema as database_name,
       tab.table_name
from information_schema.tables tab
left join information_schema.table_constraints tco
          on tab.table_schema = tco.table_schema
          and tab.table_name = tco.table_name
          and tco.constraint_type = 'PRIMARY KEY'
where tco.constraint_type is null
      and tab.table_schema not in('mysql', 'information_schema', 
                                  'performance_schema', 'sys')
      and tab.table_type = 'BASE TABLE'
--      and tab.table_schema = 'sakila' -- put schema name here
order by tab.table_schema,
         tab.table_name;

_Originally posted by @webnoob in https://github.com/prisma/prisma/discussions/11028_

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
webnoobcommented, Jan 12, 2022

Indeed, that’s my assessment too. It was baffling me at first.

1reaction
webnoobcommented, Jan 12, 2022

Do you mean the SQL to create the actingAs table?

CREATE TABLE `_acting_as` (
  `A` int NOT NULL,
  `B` int NOT NULL,
  UNIQUE KEY `_acting_as_AB_unique` (`A`,`B`),
  KEY `_acting_as_B_index` (`B`),
  CONSTRAINT `_acting_as_ibfk_1` FOREIGN KEY (`A`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `_acting_as_ibfk_2` FOREIGN KEY (`B`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

With regards to the comment which confused you. What I mean is that when I look in MySQL workbench, it shows both columns A and B as PK : image

Note: I’ve since updated my table names so _acting_as is replacing actingAs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prisma schema API (Reference)
API reference documentation for the Prisma Schema Language (PSL).
Read more >
Using custom model and field names (Concepts)
You can "rename" fields and models that are used in the Prisma Client by mapping them to the "original" names in the database...
Read more >
Data model (Reference)
Models: Represent the entities of your application domain; Map to the tables (relational databases like PostgreSQL) or collections (MongoDB) in your database ...
Read more >
Relations (Reference)
These fields are used to generate the Prisma Client. The scalar authorId field, ... A primary key column in the User table named...
Read more >
Supported database types and features
Prisma Migrate also supports mapping each field to a specific native type, and there are ways to include features without a Prisma schema...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found