Unique constraint isn't working properly
See original GitHub issueIssue 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:
- Created 4 years ago
- Comments:7
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@retorres9 I have the same error, How to fix it ?
@pabagan to make a column unique you have to add somethin like this.