0

I'm trying to create a model in SQLAlchemy, but I'm having a hard time finding out what is the best way. I currently have a class called returns, which I want to give an additional variable. A state of which the return is in. So for example, a return can be expected, received or processed. However, in the Flask application I want to show the user a nice string. For example; processed should become "Waiting for reimboursment".

The problem however is, I don't want to send these strings to the database, since I might change them in the future or add statusses. Therefore I want some kind of translation to be made between the value saved in the DB and the 'string' value. I have tried solving this by using Enums, but it is not possible to create the 'string' values. I would like something like this to return either the 'key' or the 'value', where only the key is saved in the database.

return.status.key return.status.value

I have tried looking for a solution but was not able to find anything that seems to be fit.

What is the best practice for these kinds of requirements?

RD993
  • 59
  • 1
  • 2
  • Does this answer your question? [Best way to do enum in Sqlalchemy?](https://stackoverflow.com/questions/2676133/best-way-to-do-enum-in-sqlalchemy) – Abdul Aziz Barkat Nov 28 '22 at 15:43
  • I think this is kind of the way I'm looking for, except that it tries to store a tuple in the DB for the Enum. Is this common practice? – RD993 Nov 29 '22 at 08:52
  • Tuple? There's a straightforward `Enum` class you can use in SQLAlchemy, there are answers that demonstrate that. – Abdul Aziz Barkat Nov 29 '22 at 08:54
  • Yes, I've seen that, but there is a remark that the string values will be put in the database. Isn't it a better way to store the integer values? – RD993 Nov 29 '22 at 12:23

0 Answers0