Skip to content

Conversation

@itsbekas
Copy link

@itsbekas itsbekas commented Apr 4, 2024

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.

@itsbekas itsbekas marked this pull request as ready for review April 4, 2024 20:35
@itsbekas itsbekas marked this pull request as draft April 4, 2024 20:42
@itsbekas
Copy link
Author

itsbekas commented Apr 4, 2024

I realize now that it might be worth mentioning that the reason I wanted the default value was because setting the max_length metadata wasn't working. After some digging I ended up finding this pull request: #857 which fixes that part of the problem, so maybe these 2 could be merged as they fix different ends of the same problem.

@itsbekas itsbekas marked this pull request as ready for review April 4, 2024 21:55
@stevleibelt
Copy link

Damn, so I just ran into this issue when I've executed alembic upgrade head so far.

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.

@jaspermohl
Copy link

Any estimation when this will be merged?
Running also into the issues with MariaDB and the changes make it work for me as well.

YuriiMotov

This comment was marked as resolved.

@YuriiMotov YuriiMotov changed the title use mysql's default string length for mariadb ♻️ Use mysql's default string length for mariadb Aug 22, 2025
@github-actions

This comment was marked as resolved.

@github-actions github-actions bot closed this Sep 22, 2025
@YuriiMotov

This comment was marked as outdated.

@YuriiMotov YuriiMotov reopened this Sep 27, 2025
@YuriiMotov YuriiMotov changed the title ♻️ Use mysql's default string length for mariadb ✨ Use mysql's default string length for mariadb Sep 27, 2025
@YuriiMotov YuriiMotov added feature New feature or request and removed refactor labels Sep 27, 2025
@github-actions github-actions bot removed the waiting label Sep 27, 2025
Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:[email protected]: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)
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants