Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I inform sqlalchemy to raise an error whenever MySQL throws a warning?

I want to modify sqlalchemy settings to raise an exception on warnings.

For example, when I insert a larger amount of data in a field than the defined column length, mysql will truncate the data, insert the truncated data into the field and issue a warning.

Instead of the warning, I would like sqlalchemy to raise an appropriate error on behalf of mysql.

like image 716
shahjapan Avatar asked Nov 08 '11 09:11

shahjapan


1 Answers

To turn the “oh by the way I mangled your data” warnings into errors, set an appropriate server mode for MySQL. With SQLAlchemy, you should choose ANSI, as you probably don't have to worry about your old queries being compatible with some brokenMySQL-specific behavior.

See the SQLAlchemy docs on how to do it.


(Also consider using a database that's strict by default. Postgres comes to mind.)

like image 81
Petr Viktorin Avatar answered Sep 28 '22 16:09

Petr Viktorin