Defining the class:
from sqlmodel import Field, SQLModel, create_engine, Session, select, Column,String
class person(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str = Field(max_length=100)
nickname: str = Field(max_length=30)
sqlmodel creates the table person:
CREATE TABLE person(
id SERIAL NOT NULL,
name varchar NOT NULL,
nickname varchar NOT NULL,
PRIMARY KEY(id)
);
If using sqlalchemy :
Base = declarative_base()
class person(Base):
tablename = ‘person’
id = Column(Uuid, primary_key=True, default=uuid4)
nickname = Column(String(32))
name = Column(String(100))
fields are created varchar(n)
How i fixed it for char(n) ?
2
Answers
char(n) isn’t supported. You could technically use raw SQL but ideally you would just not use char(n). Just update the column type instead.
SQLALchemy’s CHAR datatype will create a
char(n)
column in PostgreSQL. This scriptwill emit this DDL:
We can confirm the result with psql:
The final
SELECT
query produces this output:However, as noted in the comments [1], [2],
CHAR(n)
should not be used in modern PostgreSQL.