jowilf / starlette-admin

Fast, beautiful and extensible administrative interface framework for Starlette & FastApi applications
https://jowilf.github.io/starlette-admin/
MIT License
596 stars 59 forks source link

DOC: how to properly handle ADD of entity with conflicting primary key (email) #543

Closed sglebs closed 4 months ago

sglebs commented 5 months ago

Is your feature request related to a problem? Please describe.

I would like to see in the documentation the guidelines to deal with data entry. In my case, I am trying to add a USER object to the database, where the email is the primary key, and I am getting a "500/exception" because SQLAlchemy is properly flagging the invalid new record because these is already a USER with the same email (primary key).

Describe the solution you'd like

Just some example or doc that explains how to deal with data entry, including SAVE errors. It could be a NEW entity or an UPDATE (say, I change the guy's email).

Describe alternatives you've considered

I know there's the #before_create and #before_edit methods which I already use for some data validation. Should I do it in here? In the #before_create? Do a database query in #before_create and check?

Additional context None

jowilf commented 5 months ago

I know there's the #before_create and #before_edit methods which I already use for some data validation. Should I do it in here? In the #before_create? Do a database query in #before_create and check?

You can also catch the Exception. e.g: https://github.com/jowilf/starlette-admin/blob/7465db977d748baa43c3f39a20a307c3636bd7be/examples/sqla/views.py#L66

hasansezertasan commented 4 months ago

Somewhat related: #451

I think before_edit and before_create checks feels good.

I believe you could also implement a custom field for each unique columns and field types and use it instead.