0

I am trying to add a new row to my table enjordplatform_keywords but everytime I do that it instead tries to add the row in the middle of the table.
Usually that id that it is trying to insert it in is taken but sometimes (if I have removed a row say row 81 it will add the new values in the empty row 81). Though the table goes up to 474 so why doesn't it add it in 475 instead of in the middle of the table.
This happens if I try to do it through sqlalchemy and if I try to do it directly in the database. It has worked perfectly fine but then this happened for some unknown reason.

What could be the problem?

This is the table in sqlalchemy:

class enjordplatformKeywords(Base):
    __tablename__ = "enjordplatform_keywords"
    id = Column(Integer, primary_key=True, unique=True)
    keyword = Column(String)
    keyword_swedish = Column(String)
    question_id = Column(Integer,ForeignKey("enjordplatform_quiz.id"))
    times_clicked = Column(Integer)
    added_by_company = Column(Integer)
    english_added = Column(Integer)

Here is an example of when I am inserting a new record directly into the postgres database and the error it gives.

My insert

insert into enjordplatform_keywords (keyword, keyword_swedish) VALUES ('Artificial Intelligence', 'Artificial Intelligence');

The error message

ERROR:  duplicate key value violates unique constraint "enjordplatform_keywords_pkey"
DETALJ:  Key (id)=(86) already exists.

The postgres database has 430 rows and previously it would get id 431 when inserting a new record instead of trying to override id=86

7
  • 1
    a table has no beginning or end as all tables a re by definition unsorted Commented Mar 15, 2023 at 21:22
  • Ok fine. But I am guessing you understand what I mean. Now it is adding it on id 81 instead of id 473 which it has done everytime up until then. Why if I am doing an insert into to the postgres database it tries to override an id that is already existing instead of inserting it at the highest possible id Commented Mar 15, 2023 at 21:29
  • 1
    1) SQL data is unordered so a particular row can end up anywhere. 2) You need to show the code(as update to question) where you are saving the object. Commented Mar 15, 2023 at 21:34
  • Hi Ok thanks for the clarification. I have updated the question with the response when I am trying to insert a new record into the table.Thanks Commented Mar 15, 2023 at 21:40
  • 1
    See here and here Commented Mar 16, 2023 at 6:52

0

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.