New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should Country be orderable? #360
Comments
Ordering by code makes sense. You can use total_ordering decorator. PRs welcome |
Digging a bit further, I have seen what is the problem, and I have found two feasible solutions: The first one is, as previously proposed, to implement
This is a way to solve the problem, but it adds some semantics that might be completely undesired. The cause of the problem is that In this second solution,
|
Happy to do. Just a question: Should Country be comparable to any str, or just Country to Country?
|
Suppose that your models must support Persons which might have many Nationalities and you decide to proceed using a
Person
class and anotherNationality
class to represent the nationalities of persons by means of a foreign key column to the person id and a CountryType column to represent the related nacionality:No orphan is desired, therefore
ondelete='CASCADE'
is used on the foreign key, as well ascascade='all, delete-orphan'
is used on the relationship.Suppose that john is added and commited:
At this point, if one tries to delete
john
, the behaviour would depend on whether the object is loaded on the session or not. If the object is not loaded, the deletion will succeed, because the RDBMS is in charge of deleting the related rows and it knows how to do that:But if the object is loaded in the session, a
TypeError: unorderable types: Country() < Country()
exception raises:It seems that SQLAlchemy needs to know how to order Country objects, but Country is not orderable. Should methods like
__lt__
be implemented inCountry
? Which criteria should be used? Maybe alphabetically comparing Country.code?The text was updated successfully, but these errors were encountered: