将记录输入 Postgres 时出现验证错误
Validation error while inputting a record into Postgres
我使用以下查询创建了一个 table,
CREATE TABLE user_account (
user_id serial PRIMARY KEY,
user_name VARCHAR ( 50 ) UNIQUE NOT NULL,
password VARCHAR ( 50 ) NOT NULL,
email VARCHAR ( 255 ) UNIQUE NOT NULL,
created_on TIMESTAMP NOT NULL,
allow BOOLEAN NOT NULL
);
以下是我的models.py
class AccountsInfo(Base):
__tablename__ = "user_account"
user_id = Column(Integer, primary_key=True, index=True)
user_name = Column(String)
password = Column(String)
email = Column(String)
created_on = Column(DateTime)
allow = Column(Boolean)
以下是我的schema.py
class AccountsInfoBase(BaseModel):
user_name: str
password: str
email: str
created_on: str
allow: bool
class Config:
orm_mode = True
class AccountsCreate(AccountsInfoBase):
password = str
class AccountsInfo(AccountsInfoBase):
user_id: int
class Config:
orm_mode = True
我使用以下代码创建用户,
def create_user(db: Session, user: schemas.AccountsCreate):
db_user = models.AccountsInfo(user_name=user.user_name, password=user.password, email=user.email,created_on=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),allow=True)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
问题是我收到以下错误,
raise ValidationError(errors, field.type_)
pydantic.error_wrappers.ValidationError: 1 validation error for
AccountsInfo response -> created_on str type expected
(type=type_error.str)
我错过了什么?
raise ValidationError(errors, field.type_) pydantic.error_wrappers.ValidationError: 1 validation error for AccountsInfo response -> created_on str type expected (type=type_error.str)
在您的 table 中,created_on 是 DateTime。
class AccountsInfo(Base):
__tablename__ = "user_account"
created_on = Column(DateTime)
但是您在 Pydantic 模型中声明为 str
。
class AccountsInfoBase(BaseModel):
created_on: str
将您的字段声明为 datetime in Pydantic 模型。 SQLAlchemy 还使您能够自动创建日期时间。
from sqlalchemy.sql import func
time_created = Column(DateTime(timezone=True), server_default=func.now())
我使用以下查询创建了一个 table,
CREATE TABLE user_account (
user_id serial PRIMARY KEY,
user_name VARCHAR ( 50 ) UNIQUE NOT NULL,
password VARCHAR ( 50 ) NOT NULL,
email VARCHAR ( 255 ) UNIQUE NOT NULL,
created_on TIMESTAMP NOT NULL,
allow BOOLEAN NOT NULL
);
以下是我的models.py
class AccountsInfo(Base):
__tablename__ = "user_account"
user_id = Column(Integer, primary_key=True, index=True)
user_name = Column(String)
password = Column(String)
email = Column(String)
created_on = Column(DateTime)
allow = Column(Boolean)
以下是我的schema.py
class AccountsInfoBase(BaseModel):
user_name: str
password: str
email: str
created_on: str
allow: bool
class Config:
orm_mode = True
class AccountsCreate(AccountsInfoBase):
password = str
class AccountsInfo(AccountsInfoBase):
user_id: int
class Config:
orm_mode = True
我使用以下代码创建用户,
def create_user(db: Session, user: schemas.AccountsCreate):
db_user = models.AccountsInfo(user_name=user.user_name, password=user.password, email=user.email,created_on=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),allow=True)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
问题是我收到以下错误,
raise ValidationError(errors, field.type_) pydantic.error_wrappers.ValidationError: 1 validation error for AccountsInfo response -> created_on str type expected (type=type_error.str)
我错过了什么?
raise ValidationError(errors, field.type_) pydantic.error_wrappers.ValidationError: 1 validation error for AccountsInfo response -> created_on str type expected (type=type_error.str)
在您的 table 中,created_on 是 DateTime。
class AccountsInfo(Base):
__tablename__ = "user_account"
created_on = Column(DateTime)
但是您在 Pydantic 模型中声明为 str
。
class AccountsInfoBase(BaseModel):
created_on: str
将您的字段声明为 datetime in Pydantic 模型。 SQLAlchemy 还使您能够自动创建日期时间。
from sqlalchemy.sql import func
time_created = Column(DateTime(timezone=True), server_default=func.now())