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

Error: Index already exist #931

Closed
Redskinsjo opened this issue Apr 19, 2024 · 5 comments
Closed

Error: Index already exist #931

Redskinsjo opened this issue Apr 19, 2024 · 5 comments
Labels
question Qustions about how to use stuff stale This Issue | PR had no activity in a while and will be closed if it stays so

Comments

@Redskinsjo
Copy link

Redskinsjo commented Apr 19, 2024

Hi,

I have always an issue of index generation with conflicts. I try to define explicitly the name of the index, but it seems mongoose doesn't take that into account.

My code:

export {};

import { index } from "@typegoose/typegoose";
import * as typegoose from "@typegoose/typegoose";
import type { Avatars } from "./types";
import { BetaUserSchema } from "./betaUsers";
import { BetaCompetencySchema } from "./betaCompetencies";
import { JobSchema } from "./jobs";
import mongoose from "mongoose";
mongoose.connect(`${process.env.MONGODB_URI}`);
mongoose.Promise = global.Promise;

@typegoose.modelOptions({
  schemaOptions: {
    timestamps: true,
    id: true,
  },
  options: {
    automaticName: false,
    customName: "BetaCandidate",
    allowMixed: typegoose.Severity.ALLOW,
  },
})

// tried also @index({ userId: 1 }, { name: "betacandidates_userid", unique: true })
export class BetaCandidateSchema {
  @typegoose.prop()
  public avatars: Avatars[];
  @typegoose.prop({ default: [], ref: () => "BetaCompetency" })
  public competencyIds: typegoose.Ref<BetaCompetencySchema>[];
  @typegoose.prop()
  public salaryExpected: string;
  @typegoose.prop()
  public type: string;
  @typegoose.prop()
  public bgImage: string;
  @typegoose.prop({ default: false })
  public confirmed: boolean;
  @typegoose.prop({
    ref: () => "BetaUser",
    index: { name: "betacandidates_userid", unique: true },
  })
  public userId: typegoose.Ref<BetaUserSchema>;
  @typegoose.prop({ ref: () => "Job" })
  public targetJobId: typegoose.Ref<JobSchema>;
  @typegoose.prop()
  public targetContractType: string;
  @typegoose.prop()
  public createdAt: Date;
  @typegoose.prop()
  public updatedAt: Date;
}

const model =
  mongoose.models.BetaCandidate ||
  typegoose.getModelForClass(BetaCandidateSchema);

model.createIndexes();

export default model;

Versions

I am using version ^12.2.0

  • System: macos
  • NodeJS: 20.9.0
  • Typescript: ^5.4.4
    transpileOnly is not used
  • Compiler / Transpiler: ts-node
  • mongoose: ^8.3.0
  • mongodb: not imported
  • grahpql:^16.8.1

@Redskinsjo Redskinsjo added the question Qustions about how to use stuff label Apr 19, 2024
@Redskinsjo
Copy link
Author

the error is MongoServerError: Index already exists with a different name: betacandidates_userId_key

@hasezoey
Copy link
Member

hasezoey commented Apr 19, 2024

did you already try clearing the collections after adding your explicit name index?

also note that createIndexes will only create indexes, and error if they already exist (like under a different name).
maybe consider using syncIndexes instead.

if your problem still continues after deleting the initial collection (from before having added the explicit index name) consider disabling autoIndex, see Model.init.

finally, also note that the following code will not recompile the model on something like a hot-reload:

const model =
  mongoose.models.BetaCandidate ||
  typegoose.getModelForClass(BetaCandidateSchema);

it will continue to use the old version (if the model / class was changed before the reload)

@Redskinsjo
Copy link
Author

Redskinsjo commented Apr 19, 2024

@hasezoey great answer from you, thanks. I have just implemented a method to check indexes by looping over them with model.collection.getIndexes()and dropping them when necessary (if name is not corresponding). I will check later if errors raise again.

Nevertheless I will investigate your solution thoroughly to learn more...

@Redskinsjo
Copy link
Author

Please leave the issue open for now, I will come back to it and close it very soon if it's resolved ! thank you !

Copy link

Marking Issue as stale, will be closed in 7 days if no more activity is seen

@github-actions github-actions bot added the stale This Issue | PR had no activity in a while and will be closed if it stays so label May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Qustions about how to use stuff stale This Issue | PR had no activity in a while and will be closed if it stays so
Projects
None yet
Development

No branches or pull requests

2 participants