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

Use sqladmin with sqlalchemy core #831

Open
1 task done
sheldygg opened this issue Oct 12, 2024 · 3 comments
Open
1 task done

Use sqladmin with sqlalchemy core #831

sheldygg opened this issue Oct 12, 2024 · 3 comments

Comments

@sheldygg
Copy link
Contributor

Checklist

  • There are no similar issues or pull requests for this yet.

Is your feature related to a problem? Please describe.

I want use sqladmin with sqlalchemy core

Describe the solution you would like.

Looks like we must rewrite many code

Describe alternatives you considered

No response

Additional context

No response

@aminalaee
Copy link
Owner

Please provide a minimal example with the error of what is happening

@sheldygg
Copy link
Contributor Author

sheldygg commented Oct 14, 2024

from sqlalchemy import Column, Integer, String, create_engine, Table, MetaData
from fastapi import FastAPI
from sqladmin import Admin, ModelView

engine = create_engine(
    "sqlite:///example.db",
    connect_args={"check_same_thread": False},
)

metadata = MetaData()

User = Table(
    "users",
    metadata,
    Column("id", Integer, primary_key=True),
    Column("name", String),
)
metadata.create_all(engine)

class UserAdmin(ModelView, model=User):
    column_list = [User.columns.id, User.columns.name]

app = FastAPI()
admin = Admin(app, engine)

admin.add_view(UserAdmin)
  File "/Users/sheldy/projects/test-sqladmin/test.py", line 20, in <module>
    class UserAdmin(ModelView, model=User):
  File "/Users/sheldy/projects/test-sqladmin/venv/lib/python3.11/site-packages/sqladmin/models.py", line 83, in __new__
    if not model:
  File "/Users/sheldy/projects/test-sqladmin/venv/lib/python3.11/site-packages/sqlalchemy/sql/elements.py", line 748, in __bool__
    raise TypeError("Boolean value of this clause is not defined")
TypeError: Boolean value of this clause is not defined

I read sqladmin sources and realized that your code is addicted to orm

@sheldygg
Copy link
Contributor Author

sheldygg commented Oct 14, 2024

If I change if not model condition to if model is None I will receive the following error

  File "/Users/sheldy/projects/test-sqladmin/test.py", line 20, in <module>
    class UserAdmin(ModelView, model=User):
  File "/Users/sheldy/projects/test-sqladmin/venv/lib/python3.11/site-packages/sqladmin/models.py", line 93, in __new__
    cls.pk_columns = get_primary_keys(model)
                     ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sheldy/projects/test-sqladmin/venv/lib/python3.11/site-packages/sqladmin/helpers.py", line 176, in get_primary_keys
    return tuple(inspect(model).mapper.primary_key)
                 ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Table' object has no attribute 'mapper'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants