database – Is it a bad practice to couple database validation changes based on the API response code?

Let's say I have an api endpoint with some service features like

`` `python
# API endpoint
def create_a_car ():
service.create_wheels ()
service.create_engine ()
... # etc

return response (result = car, status_code = 200)

# a service
def create_wheels ():
# create wheels
session.flush ()
# session.commit ()
... # do something with the wheels.

def create_engine ():
# create wheels
session.flush ()
# session.commit ()
... # do something with

# middleware
def commit:
if response.status_code == 200:
session.commit ()
without error:
session.rollback ()
`` `

The reason I have a middleware that performs validation, is because I want to limit the number of validation calls, because the transactions would be closed after each validation, and have to restart them at each time seems useless. Moreover, because create_a_car () depends on different database operations, having individual commits in each of these functions can result in an inconsistent state of the database. for example create_wheels () successful but create_engine () failed. Now, there are non-associated wheels in the database. & # 39;

But to couple the logic of the database with the HTTP status codes seems a bit dangerous? As they are different things, or would not it matter in this case? Are there better approaches?