Naresh
Naresh

Reputation: 1962

SQL Alchemy creates new object with existing Primary key

I am trying to create new object in sqlalchemy but getting integrity error because that ID is already exist. Below is my model.

class RFBOpportunityDetail(Base):
    __tablename__ = 'rfb_opportunity_detail'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    code = Column(String, unique=True)

I am using postgreSQL Database and that table already have entries. below is code to create new object.

models.RFBOpportunityDetail(name=name, code=code)

Is there anything which i should know about creating a new object with SQLalchemy or PostgreSQL?

Error

sqlalchemy.exc.IntegrityError: (psycopg2.IntegrityError) duplicate key value violates unique constraint "fbo_all_opportunity_detail_pkey"
DETAIL:  Key (id)=(221418) already exists

Upvotes: 3

Views: 1384

Answers (1)

Naresh
Naresh

Reputation: 1962

Found a solution seems like there was some issue with postgres as i imported data through sql. i used below command to resolve this issue

SELECT setval('rfb_opportunity_detail_id_seq', MAX(id)) FROM rfb_opportunity_detail;

Upvotes: 3

Related Questions