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.

Support indexing a column partially, i.e. indexing a subset of a text field in MySQL

See original GitHub issue

Problem

I’m trying out Prisma new native types feature.

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["nativeTypes"]
}

After I introspect the db, I get the following error.

image

But actually mediumtext can be used as unique if you specify a length to it.

Suggested solution

Maybe Prisma could support something like the following:

model StatusUpdate {
  id         String               @id @db.Char(30)
  uid        String               @unique(191) @db.MediumText
}

Additional context

Prisma 1 @unique uses mediumtext unique with 191 length.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kgoggincommented, Mar 29, 2021

I ran into this also. Our DB started life with Prisma 1, and so we’ve got several places where P1 created tables with mediumtext columns marked as @unique that have an index defined that selects a sub part of that field, so that it can be unique. See shortName column in the sql below, which was exported from our P1 DB:

CREATE TABLE `Theme` (
  `id` char(30) CHARACTER SET utf8 NOT NULL,
  `shortName` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `displayName` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `primaryColor` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL,
  `logoUrl` mediumtext COLLATE utf8mb4_unicode_ci,
  `createdAt` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `updatedAt` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`id`),
  UNIQUE KEY `shortName_UNIQUE` (`shortName`(191))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

We’ve since switched over to P2, and now we’re trying to start using the native DB types via introspect, and then switch over to prisma migrate. But, when I introspect it strips the @unique from the schema and won’t let me re-add it. I see the same error @chanlito posted above. This is blocking us from being able to upgrade to the latest Prisma version, and from using Prisma Migrate. Any idea if/when the suggested approach might be implemented?

0reactions
janpiocommented, Dec 23, 2021

We released support for this in https://github.com/prisma/prisma/releases/tag/3.5.0 - please test it out @kgoggin @chanlito and let us know if this works for you - or not. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

8.3.1 How MySQL Uses Indexes
Indexes are used to find rows with specific column values quickly. Without an index, MySQL must begin with the first row and then...
Read more >
Chapter 4. Query Performance Optimization - O'Reilly
MySQL can sometimes use an index to avoid reading row data, when the index contains all the columns the query needs. We discussed...
Read more >
Efficient Use of PostgreSQL Indexes | Heroku Dev Center
A partial index covers just a subset of a table's data. It is an index with a WHERE clause. The idea is to...
Read more >
mysql - Indexing on a fixed-length string (10 characters
In a table with thousands of phone numbers and the associated user_id, I wish to be able to rapidly query the database and...
Read more >
Hibernate Search 6.1.7.Final: Reference Documentation
3, @FullTextField maps a property to a full-text index field with ... i.e. property types that are supported out of the box when ......
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