I have a table with the following declarative definition:
class Type(Base): __tablename__ = 'Type' id = Column(Integer, primary_key=True) name = Column(String, unique = True) def __init__(self, name): self.name = name
There is a unique restriction in the "name" column, but I can do
type1 = Type('name1') session.add(type1) type2 = Type(type1.name) session.add(type2)
So, as you can see, the only restriction is not checked at all, since I added to the session object 2 with the same name.
When I do session.commit() , I get a mysql error, since the restriction is also in the mysql table.
Is it possible that sqlalchemy tells me in advance that I cannot do this or does not identify it and does not insert 2 records with the same "name" columm? If not, should all existing names be stored in memory, so I can check if they exist due to the absence before creating the object?
source share