0

My table:

class api(Base):
    __tablename__ = "api"

    id = Column(Integer, primary_key=True)
    date = Column(DateTime, nullable=False)
    gender = Column(String)
    age = Column(String)
    value = Column(Integer)

Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)

When I send an api query with dates which overlaps with previous queries duplicate rows are created. How can I avoid that? I can delete duplicates but is there a more efficient way to avoid inserting them in the first place?

2 Answers 2

1

It looks like you are using SQLAlchemy ORM. You can add unique=True to a single column to create constraint on your table. For example:

class API(Base):
    ...
    date = Column(DateTime, nullable=False, unique=True)

You can also create unique constraint on multiple columns by passing UniqueConstraint to __table_args__. For example:

class API(Base):
    __table_args__ = (
        UniqueConstraint('date', 'gender'),
    )

It is still recommended that you should check the existence before you inserting the new record into the database, since a violation of database constraint will normally be logged in the database log file.

Sign up to request clarification or add additional context in comments.

2 Comments

I have multiple entries for one date because of different gender and age. So I guess I need to use your second suggestion but adding age there too... I'll try that and come back to you. Thanks!
Actually the correct approach is the other way round. You have to try to insert and catch the integrity error when the exception fails. Eliminates the need for two queries and also avoids another thread inserting between your select and insert.
0

You could create a database constraint that forbids duplicate entries and handle errors in your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.