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.

@Unique doesn't work in MongoDB

See original GitHub issue

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [x] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [ ] sqlite [ ] sqljs [ ] react-native

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

@Entity()
export class PackageSettings implements IPackageSettings {
    @ObjectIdColumn() id: ObjectID;

    @Column({ unique: true })
    @IsString()
    @IsNotEmpty()
    name: string;

    @Column('simple-json') value: object;
}

and

@Entity()
@Unique(['name'])
export class PackageSettings implements IPackageSettings {
    @ObjectIdColumn() id: ObjectID;

    @Column()
    @IsString()
    @IsNotEmpty()
    name: string;

    @Column('simple-json') value: object;
}

No index unique created in mongodb and no error thrown when I try to save multiple objects with same name attribute.

Is decorator @Unique only RDBMS-specific ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (11 by maintainers)

github_iconTop GitHub Comments

4reactions
pleerockcommented, Apr 30, 2018

You need to put synchronize: true in your connection options. This flag executes indices synchronization

3reactions
rusconcommented, Apr 27, 2018

@Index({ unique: true }) I checked on the version 0.2.3, it’s works for me

But, it’s very strange that there are 2 syntaxes @Column({ unique: true }) and @Index({ unique: true })

I use mongodb version 3.4.7

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose Unique index not working! - Stack Overflow
1 . Add unique: true to the attributes. let schema = new mongoose. · 2 . Drop the collection - for example role...
Read more >
Mongoose Unique Not Working - DEV Community ‍ ‍
The first- and best imho- is to use mongoose.connection.syncIndexes() to reconstruct the indexes of all collections to match all schemas (it ...
Read more >
Unique partial index is not working with mongoose on email
I am using mongodb version 4.4.8 and mongoose version 5.5.9. I have an user schema and this is how i define it
Read more >
Mongoose Unique Not Working?! | Have Fun Learning
1. DB already messed up. If there are already duplicated emails in your collections, then the new constraints won't work. · 2. Index...
Read more >
Unique is not working · Issue #5050 · Automattic/mongoose
HI below is my Schema in which unique is not working const user = new Schema({ userName: { type: String, required: true, unique:...
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