I am working with 2 models, a "Location" model, and a "Phone" model. There is a one-to-many relationship between them.
When a phone number is submitted, I need to format it using the phonenumbers modules, which requires a country code, which exists on the Location object. So the formatting can only happen after the flush(), as I need to have the location_id populated, so I can grab the country code from the parent Location. If the formatting of the phone number fails, I want the entire object eliminated and not written to the db. This is the current (nonworking) code I am using. @event.listens_for(Phone, 'before_insert', raw=True) @event.listens_for(Phone, 'before_update', raw=True) def save_phone(mapper, connection, target): phone = target.obj() country = object_session(phone) \ .query(Location) \ .get(phone.location_id) \ .country try: number = phonenumbers.parse(phone.number, country) phone.number = phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.E164) except: print 'failed on phone number %s' % phone.number I'm not sure how to abort the insertion/update of the object. Is this even possible? Is there something I can put in the except section to cancel the pending commit of the object in question? -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/zvjgVyGg9ZUJ. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.