Skip to content

The field name is python keywords, the field type is Mysql8.0 JSON #203

@TingHsi

Description

@TingHsi

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

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: TingHsi

from typing import Optional, List, Any

from sqlmodel import Field, Session, SQLModel, create_engine

from time import time


# the table is created in MySQL 8.0
# SQLModel.metadata.create_all(engine)


class Logs_Push(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    content: str
    createdAt: int = int(time())
    _from: str
    remark: str
    result: str
    type: str
    userIds: List[str]


engine = create_engine("mysql+cymysql://root:[email protected]:3308/db1?charset=utf8mb4")


log_1 = Logs_Push(content="log_1", createdAt=time(),_from='from',remark='remark',result='result',type='im',userIds=['0','1'])
log_2 = Logs_Push(content="log_2", createdAt=time(),_from='from',remark='remark',result='result',type='im',userIds=['2','3'])


if __name__ == '__main__':
    with Session(engine) as session:
        session.add(log_1)
        session.add(log_2)
        session.commit()

Description

  • I have a field, the name is "from",rename to "_from" that's work in sqlalchemy, but sqlmodel not support;
  • I have a field "userIds", the type is "json" in mysql8.0, use code "userIds = Column(JSON)" is work in sqlalchemy, use "userIds: List[str]" in sqlmodel not work.

Operating System

macOS

Operating System Details

No response

SQLModel Version

0.0.5

Python Version

Python 3.7.3

Additional Context

I try patch "sqlmodel/main.py" line 378 ,function get_sqlachemy_type
add code

if issubclass(field.type_, list) or issubclass(field.type_, dict):
    return JSON

And change my code "userIds: List[str]" to "userIds: Any"

Run this code return error:

if issubclass(field.type_, str):
TypeError: issubclass() arg 1 must be a class

my code not work, so i need your helps.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions