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 constraint isn't working properly

See original GitHub issue

Issue type:

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

Database system/driver:

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

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:

I’m new using typeorm and I’m having a trouble, I have created an entity that creates a user un my database, and also defined a unique constraint decorator an it still creating the user in my database when I test the Api with postman it doesn’t show any errors. user.entity.ts

@Entity()
@Unique(['username', 'ci'])
export class User extends BaseEntity {
    @PrimaryColumn()
    ci: string;

    @Column()
    username: string;

    @Column()
    @MinLength(8)
    password: string;

    @Column()
    role: UserRole;

}

user.repository.ts

async signUp(authUserDto: AuthUserDto): Promise<void> {
        const {ci, username, password} = authUserDto;

        const user = new User();
        user.ci = ci;
        user.username = username;
        user.password = password;
        user.role = UserRole.Admin;
        try {
            await user.save();
        } catch (error) {
            console.log(error.code);
        }
    }

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
simplefeelcommented, Nov 23, 2020

@retorres9 I have the same error, How to fix it ?

0reactions
retorres9commented, Nov 2, 2021

@pabagan to make a column unique you have to add somethin like this.

Column({unique: true})
columnName: string | number; //This is the name of the column you want to be unique
Read more comments on GitHub >

github_iconTop Results From Across the Web

unique Constraint doesn't work properly with space character
In char data type including unique constraints, when adding a space starting to the string in the column, unique constraint violated error ......
Read more >
WH2349 ORA SQLCD -1: HOW TO TROUBLESHOOT A ... - IBM
A unique constraint violation occurs when an UPDATE or INSERT statement attempts to insert a record with a key that already exists in...
Read more >
Django unique constraint not working properly - Stack Overflow
The whole logic of the deleted_id is working, I tested it out, the problem is, the UniqueConstraint is not working, for example:
Read more >
postgresql - Why doesn't my UNIQUE constraint trigger?
In general, a unique constraint is violated if there is more than one row in the table where the values of all of...
Read more >
An Essential Guide to SQL Server UNIQUE Constraint
Although both UNIQUE and PRIMARY KEY constraints enforce the uniqueness of data, you should use the UNIQUE constraint instead of PRIMARY KEY constraint...
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