Skip to content
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

MongoDB views cause db push error #18424

Closed
jamiter opened this issue Mar 22, 2023 · 11 comments
Closed

MongoDB views cause db push error #18424

jamiter opened this issue Mar 22, 2023 · 11 comments
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: mongodb topic: prisma db push CLI: prisma db push topic: views

Comments

@jamiter
Copy link

jamiter commented Mar 22, 2023

Prisma version: 4.11.0
Provider: MongoDB

I'm trying to set my MongoDB indexes (for @unique for example) using the db push command. I've added a MongoDB view to the database which causes this error:

Namespace <dbName.viewName> is a view, not a collection

Removing the view from the database makes db push run successfully, setting the desired indexes.

Reproduction:

  • Add a view to your MongoDB database
  • Run prisma db push

Reported this issue in this ticket earlier: #678 (comment)

Edit: It might be good to mention that this error also shows up when doing a prisma db pull --force when there is a view in the database.

@janpio
Copy link
Member

janpio commented Mar 22, 2023

Migrations, which includes db push, are not supported for views in any database yet.
That also includes adding unique constraints and indexes to MongoDB views.

@janpio janpio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/schema Issue for team Schema. topic: prisma db push CLI: prisma db push labels Mar 22, 2023
@jamiter
Copy link
Author

jamiter commented Mar 22, 2023

Thanks for your response @janpio, but I'm not trying to add an index to the view, but another model. Adding that index, after removing the view from the DB, is actually working.

To be clear, I'm not trying to migrate or push a view. But there being a view in the DB causes this issue.

@janpio
Copy link
Member

janpio commented Mar 22, 2023

Can you share an example schema that does not work, the commands required to setup the database behind it, and then a schema that does work with the same database? Thanks.

@jamiter
Copy link
Author

jamiter commented Mar 22, 2023

The schema doesn't affect the error. The fact that a view is available in the database is enough to cause an issue.

Schema:

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

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

model User {
  id     String @id @default(auto()) @map("_id") @db.ObjectId
  email  String @unique

  @@map("users")
}

Doing a db push will work as expected. A collection is created and a unique index is added to the email attribute.

To have the error occur, add a view. For example, use a seed script with the following:

  await prisma.$runCommandRaw({
    create: 'userView',
    viewOn: 'users',
    pipeline: [
      {
        $addFields: {
          foo: "bar"
        }
      },
    ]
  });

Trying to run db push after the view has been added will cause the mentioned error. Removing the view (manually) will make db push work again.

@jamiter
Copy link
Author

jamiter commented Mar 22, 2023

The solution is probably somewhere in simply ignoring views when looping over collections. The MongoDB differ is trying to collect indexes for all collections, but views do not have indexes:

https://github.com/prisma/prisma-engines/blob/78c592cc8424d6adc77df66f66b6937fd4e254a0/migration-engine/connectors/mongodb-migration-connector/src/differ.rs#L59-L64

See MongoDB documentation:
https://www.mongodb.com/docs/manual/core/views/#indexes

I'm not a Rust developer, else I would have created a PR for it. Thanks for looking into it!

@jamiter
Copy link
Author

jamiter commented May 22, 2023

2 months in and I wonder if this is being looked at.

It's as simple as filtering only the collections and ignoring the views, like in this Stack Overflow issue: https://stackoverflow.com/questions/56013171/mongodb-list-collection-without-showing-views

@jamiter
Copy link
Author

jamiter commented Jun 1, 2023

Looks like a duplicate of #16179

@jamiter
Copy link
Author

jamiter commented Jun 1, 2023

Also related: #17006

@christianledgard
Copy link

Any news on this? Having the same issue over here. @jamiter, could you let me know if you found a workaround?

@jamiter
Copy link
Author

jamiter commented Jun 18, 2023

@christianledgard, we currently remove the views before doing the db push. Our seed script ads them again afterwards. This is done automatically in our setup.

In concept it works like this:

npm run db:remove-views
npm run db:push
npm run db:seed /* adds views again */

Because the views do not contain any data directly this is not an issue. The big downside (besides being a workaround) is that the views do not exist for roughly 10 seconds, which could be an issue on production. And because we want to always be able to release, I hope this will be fixed soon.

It doesn't seem like a lot of work to fix... I tried it myself but didn't get the engines build correctly with rust on my M1.

@Jolg42 Jolg42 added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Aug 28, 2023
@Jolg42
Copy link
Member

Jolg42 commented Aug 28, 2023

Note: This is now fixed in 5.3.0-dev.9 if you want to try it out. Let us know if it works like expected.
It will be released in the official 5.3.0 version on Tuesday, September, 12th.

@Jolg42 Jolg42 closed this as completed Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: mongodb topic: prisma db push CLI: prisma db push topic: views
Projects
None yet
Development

No branches or pull requests

4 participants