Skip to content
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

How to translate query lookup string into a SQLModel object? #343

Open
8 tasks done
perfecto25 opened this issue May 17, 2022 · 0 comments
Open
8 tasks done

How to translate query lookup string into a SQLModel object? #343

perfecto25 opened this issue May 17, 2022 · 0 comments
Labels
question Further information is requested

Comments

@perfecto25
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

# parse selections and generate a lookup query
        for sel in selection:
            if sel[1] and not sel[1] == "all":
                name = sel[0].replace("sel_", "")
                val = sel[1]
                if name == "customer":
                    query = query.filter(Xconnect.customer == int(val))
                if name == "region":
                    query = query.filter(Xconnect.region == int(val))
                if name == "lvc":
                    query = query.filter(Xconnect.lvc == str(val))
                if name == "id":
                    query = query.filter(Xconnect.id == int(val))
                if name == "conn_status":
                    query = query.filter(Xconnect.conn_status == int(val))
                if name == "environment":
                    query = query.filter(Xconnect.environment == int(val))
                if name == "client_type":
                    query = query.filter(Xconnect.client_type == int(val))
                if name == "vendor":
                    query = query.filter(Xconnect.vendor == int(val))
                if name == "comp_id":
                    query = query.filter(Xconnect.comp_id == int(val))

Description

Hello, I have a select field to search for DB records based on some fields, table is called Xconnect,

how do I translate a lookup string into a SQLModel object w/o using constant If/Then blocks?

is there a better way to do this than explicit if/then statements?

for example if someone searches Xconnect table where client_type == "abc"

how can I translate this into a SQL model query w/o using explicit if/then mapping, ie

              if name == "client_type":
                    query = query.filter(Xconnect.client_type == int(val))

Operating System

Linux

Operating System Details

Ubuntu 20.04

SQLModel Version

0.0.6

Python Version

3.8

Additional Context

No response

@perfecto25 perfecto25 added the question Further information is requested label May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant