3

I'm using SQLModel for FastAPI. But I don't know how to create 'text' column type using it.

How can I create 'text' column? Thank you for reading it.

from sqlmodel import SQLModel, Field


class BaseModel(SQLModel):
    col_1: str = Field(default='Y')
    col_2: str = Field(default='N')
    col_3: str = Field(default='0')
    col_4: str = Field(default='0')

this is my solution!

from sqlmodel import SQLModel, Field
from sqlalchemy import Column, TEXT


class BaseModel(SQLModel):
    col_5: str = Field(sa_column=Column(TEXT))
ynjo
  • 97
  • 6
  • You dont need to force it to be TEXT, by default it will make the column VARCHAR which is an alias for TEXT on Postgres. Refer to SQLModel website here: https://sqlmodel.tiangolo.com/tutorial/create-db-and-table/#text-or-varchar – Zaffer May 25 '22 at 13:56

1 Answers1

2

Try this:

from sqlalchemy.dialects.postgresql import TEXT

from sqlmodel import SQLModel, Field
from sqlalchemy import Column


class BaseModel(SQLModel):
    col_5: str = Field(sa_column=Column(TEXT))
Nikita
  • 376
  • 1
  • 10