Unique Constraint Violated on Empty PostgreSQL Table Using Python SQLAlchemy

I am trying to insert data into an empty table. I can insert my dataframes for the other tables just fine, but I cannot insert it into my shipping table, which is also the only one with a unique constraint.

I get the following error messages:

IntegrityError: (psycopg2.errors.UniqueViolation) duplicate key value violates unique constraint “shipping_lid_price_currency_code_key”
DETAIL: Key (lid, price, currency_code)=(85238091, 120.0, EUR) already exists.

I have the following SQL table:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> CREATE TABLE shipping
(
sid SERIAL PRIMARY KEY,
lid BIGINT NOT NULL,
region_code TEXT NOT NULL,
region_name TEXT NOT NULL,
price NUMERIC NOT NULL CHECK (price >= 0),
currency_code CHAR(3) CHECK (currency_code IN ('USD', 'EUR', 'GBP')),
estimated_delivery_from_days INT NOT NULL, /* Earliest number of days to deliver */
estimated_delivery_to_days INT NOT NULL, /* Latest number of days to deliver */
destination_country_name TEXT NOT NULL,
destination_country_short_code TEXT NOT NULL,
combined_shipping_allowed BOOL NOT NULL,
delivery_methods TEXT NOT NULL,
extra_insurance BOOLEAN NOT NULL,
provider_id INT NOT NULL,
is_pickup_preferable BOOLEAN NOT NULL,
is_pickup_only BOOLEAN NOT NULL,
pickup_location_country_code TEXT,
pickup_location_city TEXT,
shipping_timestamp TIMESTAMP WITH TIME ZONE NOT NULL, /* Timestamp for when we request the shipping API */
FOREIGN KEY (lid) REFERENCES meta (lid) ON DELETE CASCADE,
UNIQUE (lid, price, destination_country_short_code,currency_code)
);
</code>
<code> CREATE TABLE shipping ( sid SERIAL PRIMARY KEY, lid BIGINT NOT NULL, region_code TEXT NOT NULL, region_name TEXT NOT NULL, price NUMERIC NOT NULL CHECK (price >= 0), currency_code CHAR(3) CHECK (currency_code IN ('USD', 'EUR', 'GBP')), estimated_delivery_from_days INT NOT NULL, /* Earliest number of days to deliver */ estimated_delivery_to_days INT NOT NULL, /* Latest number of days to deliver */ destination_country_name TEXT NOT NULL, destination_country_short_code TEXT NOT NULL, combined_shipping_allowed BOOL NOT NULL, delivery_methods TEXT NOT NULL, extra_insurance BOOLEAN NOT NULL, provider_id INT NOT NULL, is_pickup_preferable BOOLEAN NOT NULL, is_pickup_only BOOLEAN NOT NULL, pickup_location_country_code TEXT, pickup_location_city TEXT, shipping_timestamp TIMESTAMP WITH TIME ZONE NOT NULL, /* Timestamp for when we request the shipping API */ FOREIGN KEY (lid) REFERENCES meta (lid) ON DELETE CASCADE, UNIQUE (lid, price, destination_country_short_code,currency_code) ); </code>
    CREATE TABLE shipping
(
  sid SERIAL PRIMARY KEY,
  lid BIGINT NOT NULL,
  region_code TEXT NOT NULL,
  region_name TEXT NOT NULL,
  price NUMERIC NOT NULL CHECK (price >= 0),
  currency_code CHAR(3) CHECK (currency_code IN ('USD', 'EUR', 'GBP')),
  estimated_delivery_from_days INT NOT NULL, /* Earliest number of days to deliver */
  estimated_delivery_to_days INT NOT NULL, /* Latest number of days to deliver */
  destination_country_name TEXT NOT NULL,
  destination_country_short_code TEXT NOT NULL,
  combined_shipping_allowed BOOL NOT NULL,
  delivery_methods TEXT NOT NULL,
  extra_insurance BOOLEAN NOT NULL,
  provider_id INT NOT NULL,
  is_pickup_preferable BOOLEAN NOT NULL,
  is_pickup_only BOOLEAN NOT NULL,
  pickup_location_country_code TEXT,
  pickup_location_city TEXT,
  shipping_timestamp TIMESTAMP WITH TIME ZONE NOT NULL, /* Timestamp for when we request the shipping API */
  FOREIGN KEY (lid) REFERENCES meta (lid) ON DELETE CASCADE,
  UNIQUE (lid, price, destination_country_short_code,currency_code)
);

Where lid is the primary key for the meta table.

I am able to correctly insert data into all the other tables in my database, and I am occasionally able to insert data into the shipping table. Although there seemed to be no common denominator between the data I was able to insert, that could differentiate it from my other data.

I use Pandas dataframes to insert the data as such:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>engine = create_engine('postgresql://DATABASE:PASSWORD@localhost:5432/USERNAME')
Session = sessionmaker(bind=engine)
session = Session()
"""
SECRET CODE THAT GENERATES MY DATAFRAME
"""
df.to_sql(record_key.replace("_record",""), con=engine, if_exists='append',index=False)
</code>
<code>engine = create_engine('postgresql://DATABASE:PASSWORD@localhost:5432/USERNAME') Session = sessionmaker(bind=engine) session = Session() """ SECRET CODE THAT GENERATES MY DATAFRAME """ df.to_sql(record_key.replace("_record",""), con=engine, if_exists='append',index=False) </code>
engine = create_engine('postgresql://DATABASE:PASSWORD@localhost:5432/USERNAME')
Session = sessionmaker(bind=engine)
session = Session()

"""
SECRET CODE THAT GENERATES MY DATAFRAME

"""

df.to_sql(record_key.replace("_record",""), con=engine, if_exists='append',index=False)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật