Sample Header Ad - 728x90

Checking for already present data before commiting?

0 votes
1 answer
67 views
I'm following a simple youtube course on how to build a FastAPI app with a Postgres db. The Users table has the following schema (using SQLAlchemy): class User(Base): """ Class responsible for the users table in the PostgreSQL DB """ __tablename__ = "users" id = Column(Integer, primary_key=True, index=True, nullable=False) email = Column(String, unique=True, nullable=False) password = Column(String, nullable=False) created_at = Column(TIMESTAMP(timezone=True), nullable=False, server_default=text("now()")) I've noticed that when I try to create a user (a row in a table) with an already used mail, I get an error, which is expected. Then, if I change the mail to a different one, and commit again the change, all is fine... However, when I check in the table for all the elements, I see that the id column has skipped some values, corresponding to those tries for which I got sqlalchemy.exc.IntegrityError. Given this situation, I wonder whether it's better to first check for an already existing mail, and only then try to commit... otherwise, the id column will have jumps in its values.
Asked by An old man in the sea. (117 rep)
May 29, 2023, 08:38 AM
Last activity: May 30, 2023, 12:18 PM