✨ Use mysql's default string length for mariadb#882
✨ Use mysql's default string length for mariadb#882itsbekas wants to merge 8 commits intofastapi:mainfrom
Conversation
|
I realize now that it might be worth mentioning that the reason I wanted the default value was because setting the |
|
Damn, so I just ran into this issue when I've executed This is really sad since - if I get it right - this will stop me from using sqlmodel in my current project and switch back to sqlalchemy :(. Thanks for your PR. |
|
Any estimation when this will be merged? |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as outdated.
This comment was marked as outdated.
YuriiMotov
left a comment
There was a problem hiding this comment.
LGTM
I attempted to add a test, but failed to make it working.
The idea was to create DDL without real connection using ddl = str(CreateTable(Hero.__table__).compile(engine, dialect=dialect)), but in this case dialect is always mysql.
I tried setting dialect.server_version_info = (10, 6, 0) with no effect.
Tested it locally with real connection and it works.
How to test locally
docker-compose.yaml
version: "3"
services:
mariadb:
image: "mariadb:10.6"
restart: 'always'
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: "mysecretpassword"
MYSQL_DATABASE: "some_db"
MYSQL_USER: "user"
MYSQL_PASSWORD: "mysecretpassword"
test.py
from sqlmodel import SQLModel, create_engine, Field
class Hero(SQLModel, table=True):
name: str = Field(primary_key=True)
database_url = "mariadb+mariadbconnector://user:mysecretpassword@127.0.0.1:3306/some_db"
engine = create_engine(database_url, echo=True)
SQLModel.metadata.drop_all(engine)
SQLModel.metadata.create_all(engine)Before applying changes it fails with sqlalchemy.exc.CompileError: (in table 'hero', column 'name'): VARCHAR requires a length on dialect mariadb, after - works:
CREATE TABLE hero (
name VARCHAR(255) NOT NULL,
PRIMARY KEY (name)
)
As I presented in #881 , mariadb doesn't have a default string size, unlike mysql, which leads to an sqlalchemy compile error. From what I understood (and tried), simply using the mysql default length and checking for the mariadb dialect.name is enough to prevent this error.