A bit of background:
I am using pyramid
framework with SQLAlchemy
. My db session is handled by pyramid_tm and ZTE
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
I have a very complicated database design with lots of model classes, foreign keys, and complex relationships between my models.
So while doing some very complicated logic on my Models and deleting, updating , inserting, and moving around objects from relationships in different models I used to get IntegrityError
randomly which would go away after restarting pserve
.
This is very strange because autoflush
is on and in theory session must be flushed as soon as I change anything on models.
So my solution to the random IntegrityError
was to manually flush the session within my logic whenever things get very complicated.
Since I did the DBSession.flush()
within my logic I haven't got the IntegrityError any more.
The question
Now I have 2 questions:
How come autoflush does not prevent from integrity error? Is it that autoflush does not clean the models but
DBSession.flush()
cleans the models?Are there any side effects of calling
DBSession.flush()
within my code? I can't really think of any side effects (apart from little performance overhead of calling DB). I don't really like callingDBSession.flush()
within my code as it is something that must really be handled by framework.
See also
Thank you.