Suppose I have a super simple table like this:
class Dinosaur(Base):
__tablename__ = 'dinosaurs'
id = Column(Integer, primary_key=True)
name = Column(String(255))
We assume that the id is set up in such a way that by default it always
gets a unique value - ie, it uses autoincrement in MySQL, or a sequence in
postgres, etc.
Now, suppose I get an instance of this class, and then delete it:
steggy = session.query(Dinosaur).filter_by(name='Steggy').one()
print steggy.id
session.delete(steggy)
session.commit()
print steggy.id
What I'd ideally like to see is that it first print the id of the row that
it pulled from the database, and then print 'None':
30
None
Is there any way that I can configure the id column / property so that it
is automatically "cleared" on delete like this?
If not, as a consolation prize, I'd also be interested in the easiest way
to query if a given instance exists in the database - ie, I could do
something like:
session.exists(steggy)
OR
steggy.exists()
...which, in this case, would simply run a query to see if any dinosaurs
exist with the name "Steggy". Needing to set up some extra parameters to
make this possible - such as adding a unique constraint on the name column
- would be potentially possible. And yes, I know I can always fall back on
just manually constructing a query against the name field myself...
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.