postgresql


UNIQUE constraint not working on a column in postgresql


Below is the schema:
BEGIN;
DROP TABLE IF EXISTS contacts;
CREATE TABLE contacts (
user_id SERIAL,
first_name VARCHAR NOT NULL,
last_name VARCHAR NOT NULL,
email VARCHAR NOT NULL,
phone_number VARCHAR NOT NULL,
status VARCHAR NOT NULL,
created_at TIMESTAMP DEFAULT current_timestamp,
UNIQUE (phone_number, email)
);
COMMIT;
and below is the code for inserting the data in database using express and pg-promise:
function createContact(req, res, next) {
db.none('insert into contacts(first_name, last_name, email, phone_number, status)' +
'values(${first_name}, ${last_name}, ${email}, ${phone_number}, ${status})',
req.body)
.then(() => {
res.status(200)
.json({
status: 'success',
message: 'Inserted one contact',
});
})
.catch(err => next(err));
}
The UNIQUE constraint doesn't seem to work on email column. I can add duplicate values in email column. The constraint works only on Phone number.
I have installed the database using EDB Postgres installer for Windows 10. I am using PGADMIN 4 and PSQL Shell for data manipulation.
UNIQUE (phone_number, email)
tells, that a pair <phone_number, email> should be unique, not each column separately.
Try to set unique constraint for separate columns.
Check out Constraints
If you have UNIQUE (phone_number, email) that means, that the touple (phone_number, email) must be unique. So you can have duplicated emails as long as they are with different phone numbers (and vice versa).
You want something like that: UNIQUE (phone_number), UNIQUE (email).

Related Links

Postgresssql Advanced Join
Slow loading of Odoo translations
WSO2 DAS Event insertion error (duplicate key value violates unique constraint) with PostgreSQL
PostgreSQL vacuuming a big table
Is there any JPA implementation that supports PostGIS Geography data type?
DBLink in PostgreSQL drops DNS name of target (Redshift), but recovers after service restart
install pljava on Windows 7 with Postgres 9.6
PGbouncer ERROR accept() failed: Too many open files
I'm trying to insert tuples into a table A (from table B) if the primary key of the table B tuple doesn't exist in tuple A
Hibernate Envers, empty Postgres table
Postgres and jsonb - search value at any key
I need to get 3 letter month as 'Jan', 'Feb' using extract () function from date column in postgresql
Using a Salt State to Query a Postgres Table and Use Return Values
How to backup hundreds of Salesforce files/schema to postgreSQL database using Talend?
Python psycopg2 - using .format() with a dbname inside a string
how to query in postgresql to find the KNN search and distance between two arrays which have approx 2000 elements each in a single column

Categories

HOME
msbuild
model-view-controller
service
cluster-analysis
dronekit
opencv4android
watson-iot
x509certificate
salesforce-lightning
assign
hiveql
esxi
xss
ebay
spring-data-solr
theory
decoding
email-attachments
expo
jsfiddle
multibinding
game-engine
css-float
ajaxcontroltoolkit
email-client
my.cnf
large-files
concrete5
undo
vtd-xml
key-bindings
ng2-bootstrap
webmock
matlab-compiler
asp.net-webhooks
repeat
sdl-2
motherboard
client-server
redhat-brms
iterm2
armadillo
mediastream
file-conversion
observer-pattern
keychain
azure-web-roles
delphi-xe
test-kitchen
datagridviewcombobox
smooth-streaming
geminabox
django-tables2
serialversionuid
intermediate-language
openpgp.js
programming-pearls
overhead
ios-keyboard-extension
copy-constructor
easy68k
ldd
scriptengine
vga
falcon
kik
window.location
cexception
skbio
instabug
bioconductor
android-facebook
windows-kernel
database-project
strcpy
pymol
touch-typing
launcher
gnip
livescript
azure-worker-roles
drawable
amzi-prolog
android-relativelayout
opencobol
data-layers
misfire-instruction
mosix
symbian3
gwt2
auctex
yii-components
configurationsection
invalidoperationexception
m3u
apache-commons-email
rjs
soapexception
sipdroid
netduino
cryptolicensing
noise-reduction
onunload
fxcopcmd
system-analysis
versions
konsole
dynamic-scope
coderush-xpress
user-preferences
disk-based

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App