-
-
Notifications
You must be signed in to change notification settings - Fork 665
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 add BIT(1) data type to field? #298
Comments
@dragonballa All the type mappings to the DB are hard coded right now and for the moment seem to be fairly limited. It should be possible to extend this method and add extra mappings to the column types you need assuming SQLAlchemy supports them. |
@oldfielj-ansto how do i import this so I can access the other fields from here? do i do
if i want to add a VARCHAR type how would I do this? i really need some assistance with this as it is basically a deal breaker for me to use SQLModel in my project or not. Really want to use this but this data type extension part im not familiar with.
|
If your bit is a boolean, you can use that SQLAlchemy type in a field declaration, https://docs.sqlalchemy.org/en/14/core/type_basics.html#sqlalchemy.types.Boolean from sqlalchemy import Column, String
from sqlmodel import Field
(...)
code: bool = Field(sa_column=Column(Boolean)) Not tested but should be like that - this is an actual thing we use to declare a pg string array from sqlalchemy import Column, String
from sqlalchemy.dialects import postgresql #ARRAY contains requires dialect specific type
from sqlmodel import Field
(...)
tags: Optional[Set[str]] = Field(default=None, sa_column=Column(postgresql.ARRAY(String()))) |
@dragonballa SQLModel is built to be as generic as possible to ensure compatibility with a wide array of database flavors, but as @antont points out you can add a specific column type override and a better supported way of doing this would be to use a boolean. Accessing an existing database schema does complicate things a little, but a solution like the following should work.
I don't have a MySQL database handy to test the behavior here, but I can tell you that by default SQLModel picks VARCHAR as the column type for PostgreSQL databases, so you might not need to override the column type, but if for some reason you needed to be explicit, something like this should work.
Also bear in mind that applying this sort of patch would mean your code would only work on MySQL databases and would need to be modified if you decided to switch to another database software such as PostgreSQL. Another thing worth noting here is that passing sa_column will mean the "primary_key", "index" and similar parameters on your field definition will not work, so if you need to pass them you'll have to manually specify them in your column definition.
My suggestion would be to install MySQL locally on your machine and start playing around creating new databases from your SQLModel's and see what column types you get, as SQLModel heavily relies on SQLAlchemy in this area. Sorry I can't give you anything more concrete, but I primarily work with PostgreSQL these days. |
First Check
Commit to Help
Example Code
Description
First of all I don't know exactly what are all the available types are so I looked at the sqlalchemy import statements over at https://github.com/tiangolo/sqlmodel/blob/7fcd4fd7c5661d2621e89ebf3a87059ac91ea6d2/sqlmodel/__init__.py
However I do not see an equivalent for MySQL's
BIT(1)
so what can I do here? I am trying to connect to an existing MySQL table and it has a data type asBIT(1)
which when I google it seems to be a Boolean? orTINYINT
Just not clear to me how I can make SQLModel work with this table containing
BIT(1)
as data type.Operating System
Linux
Operating System Details
No response
SQLModel Version
0.0.6
Python Version
3.8
Additional Context
No response
The text was updated successfully, but these errors were encountered: