GithubHelp home page GithubHelp logo

Comments (7)

shifqu avatar shifqu commented on May 13, 2024 8

I also encountered this issue and ended up implementing the check for a Literal at the beginning of get_sqlachemy_type.

field_type = field.type_
if is_literal_type(field_type):
    # Try to get the single type of the literal, error otherwise
    types = list({type(x) for x in get_args(field_type)})
    if len(types) != 1:
        raise RuntimeError("Cannot map a literal with multiple types to a sql type.")
    field_type = types[0]

To make this work you would still need to import get_args and is_literal_type, also refactor each field.type_ call to field_type

The problem with this solution is that it is specifically for a Literal. So, not sure if this will be immediately accepted to be implemented. Happy to make this into a PR if needed.

I eventually stopped using Literal, and chose for an Enum, which works out of the box :)

In your case:

from enum import Enum

class NameEnum(str, Enum):
    FASTAPI = "fastapi"
    SQLMODEL = "sqlmodel"


class Cool(SQLModel, table=True):
    name: NameEnum

from sqlmodel.

data-djinn avatar data-djinn commented on May 13, 2024 2

@shifqu this solved my problem, thanks!

from sqlmodel.

sietse avatar sietse commented on May 13, 2024 1

EDIT: See pydantic/pydantic#5821 (comment) for the main issue and a list of workarounds.

I'm deleting the rest of my comment: all its information is present in the linked thread.

from sqlmodel.

Cielquan avatar Cielquan commented on May 13, 2024

I have the same issue and went with the enum solution. But the Literal approach would be still worth adding I guess, for the future to open up the possibilities.

from sqlmodel.

Cielquan avatar Cielquan commented on May 13, 2024

I encountered an issue with the enum approach (see #406) where the validation for the field with the enum values silently passes for invalid values.

from sqlmodel.

sasilver75 avatar sasilver75 commented on May 13, 2024

I have this issue when trying to use a Discriminated Union on a SQLModel a la Pydantic

from sqlmodel.

PaleNeutron avatar PaleNeutron commented on May 13, 2024

In your case:

from enum import Enum

class NameEnum(str, Enum):
    FASTAPI = "fastapi"
    SQLMODEL = "sqlmodel"


class Cool(SQLModel, table=True):
    name: NameEnum

I have to emphasis this code actually write FASTAPI uppercase, the enum key into database, not "fastapi" lower case, the value.

from sqlmodel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.