Skip to content

Handling ForeignKeyConstraintViolationException #480

Answered by hrach
patrik-owly asked this question in Q&A
Discussion options

You must be logged in to vote

Ok, so the proper solution here is really to check the relationships upfront the entity removal

/**
 * @property int $id {primary}
 * @property OneHasMany|Boats[] $boats {Boat::$location}
 */
class Location extends Entity {}

if ($this->location->boats->countStored() > 0) {
	$this->flashMessage(
		'Lokaci nelze odstranit, jelikož je přiřazena nějaké lodi.',
		'danger',
	);
	return;
}

Why it is recommended? Orm models data as entities and their consistency should be checked upfront. Other storage implementation (like non-relational database/storing simple serialized data) won't cover this data inconsistency. So it should be modeled in Orm.


The alternative solution is to use DB checks (for…

Replies: 5 comments 7 replies

Comment options

You must be logged in to vote
1 reply
@hrach
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@hrach
Comment options

@patrik-owly
Comment options

@hrach
Comment options

@patrik-owly
Comment options

Comment options

You must be logged in to vote
2 replies
@patrik-owly
Comment options

@hrach
Comment options

hrach Feb 1, 2021
Maintainer

Answer selected by hrach
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants