简体   繁体   English

pydantic.error_wrappers.ValidationError:For Trip type=value_error.missing 的 11 个验证错误

[英]pydantic.error_wrappers.ValidationError: 11 validation errors for For Trip type=value_error.missing

Im getting this error with my pydantic schema, but oddly it is generating the object correctly, and sending it to the SQLAlchemy models, then it suddenly throws error for all elements in the model.我的 pydantic 模式出现此错误,但奇怪的是它正确生成了 object,并将其发送到 SQLAlchemy 模型,然后它突然对 model 中的所有元素抛出错误。

response -> id
  field required (type=value_error.missing)
response -> date
  field required (type=value_error.missing)
response -> time
  field required (type=value_error.missing)
response -> price
  field required (type=value_error.missing)
response -> distance
  field required (type=value_error.missing)
response -> origin_id
  field required (type=value_error.missing)
response -> destination_id
  field required (type=value_error.missing)
response -> driver_id
  field required (type=value_error.missing)
response -> passenger_id
  field required (type=value_error.missing)
response -> vehicle_id
  field required (type=value_error.missing)
response -> status
  field required (type=value_error.missing)

i must say that all the fields should have values.我必须说所有的领域都应该有价值。 And the error trace do not references any part of my code so i dont even know where to debug.而且错误跟踪不引用我的代码的任何部分,所以我什至不知道在哪里调试。 Im a noob in SQLAlchemy/pydantic我是 SQLAlchemy/pydantic 的菜鸟

here are some parts of the code这是代码的一些部分

class Trip(BaseModel):
    id: int
    date: str
    time: str
    price: float
    distance: float
    origin_id: int
    destination_id: int
    driver_id: int
    passenger_id: int
    vehicle_id: int
    status: Status

    class Config:
        orm_mode = True
class TripDB(Base):
    __tablename__ = 'trip'
    __table_args__ = {'extend_existing': True}
    id = Column(Integer, primary_key=True, index=True)
    date = Column(DateTime, nullable=False)
    time = Column(String(64), nullable=False)
    price = Column(Float, nullable=False)
    distance = Column(Float, nullable=False)
    status = Column(String(64), nullable=False)

    origin_id = Column(
        Integer, ForeignKey('places.id'), nullable=False)
    destination_id = Column(
        Integer, ForeignKey('places.id'), nullable=False)

    origin = relationship("PlaceDB", foreign_keys=[origin_id])
    destination = relationship("PlaceDB", foreign_keys=[destination_id])

    driver_id = Column(
        Integer, ForeignKey('driver.id'), nullable=False)
    vehicle_id = Column(
        Integer, ForeignKey('vehicle.id'), nullable=False)
    passenger_id = Column(
        Integer, ForeignKey('passenger.id'), nullable=False)
def create_trip(trip: Trip, db: Session):
    origin = db.query(models.PlaceDB).filter(models.PlaceDB.id == trip.origin_id).first()
    destination = db.query(models.PlaceDB).filter(models.PlaceDB.id == trip.destination_id).first()
    db_trip = TripDB(
        id=(trip.id or None),
        date=trip.date or None, time=trip.time or None, price=trip.price or None, 

    distance=trip.distance or None, 
            origin_id=trip.origin_id or None, destination_id=(trip.destination_id or None), status=trip.status or None, 
            driver_id=trip.driver_id or None, passenger_id=trip.passenger_id or None, vehicle_id=trip.vehicle_id or None, origin=origin, destination=destination)
    try:
        db.add(db_trip)
        db.commit()
        db.refresh(db_trip)
        return db_trip

    except:
        return "Somethig went wrong"

这似乎是 pydantic 模型上的一个错误,它也发生在我身上,我无法修复它,但实际上如果你只是跳过路线中的类型检查,它工作正常

It seems like there is conflict in your schema and create_trip function.您的架构和 create_trip function 似乎存在冲突。 Have you checked whether you are passing correct param to your schema?您是否检查过是否将正确的参数传递给您的架构? You have defined most of the fields as not nullable, and you are passing None as alternative value in db.add() command.您已将大多数字段定义为不可为空,并且在 db.add() 命令中将 None 作为替代值传递。

I had similar problem with my code, and I figured that naming and type convention between schema and server.py.我的代码也有类似的问题,我认为 schema 和 server.py 之间的命名和类型约定。 After matching the field names and type in both file, I resolved the error of field required (type=value_error.missing).在两个文件中匹配字段名称和类型后,我解决了所需字段的错误(type=value_error.missing)。

# project/schema.py
from pydantic import BaseModel

# here I had made mistake by using target_URL in server.py
# variable name and type should be same in both schema and server
class URLBase(BaseModel):
    target_url: str


class URL(URLBase):
    is_active: bool
    clicks: int

    class Config:
        orm_mode = True


class URLInfo(URL):
    url: str
    admin_url: str
# project/server.py
@app.post('/url', response_model=schema.URLInfo)
def create_short_url(url: schema.URLBase, db: Session = Depends(get_db)):
    if not validators.url(url.target_url):
        raise bad_request_message(message="Your provided URL is not valid!")
    chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    key = "".join(secrets.choice(chars) for _ in range(5))
    secret_key = "".join(secrets.choice(chars) for _ in range(8))
    db_url = models.URL(target_url=url.target_url, key=key, secret_key=secret_key)
    db.add(db_url)
    db.commit()
    db.refresh(db_url)
    db_url.url = key
    db_url.admin_url = secret_key

    return db_url

Please check the return statement, i had similar issue and got the issue reolved by correcting my return statement.请检查退货声明,我有类似的问题,并通过更正我的退货声明解决了问题。 I see return statement missing or wrong in create_trip function.我在 create_trip function 中看到返回语句丢失或错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pydantic.error_wrappers.ValidationError:B 的 1 个验证错误 - pydantic.error_wrappers.ValidationError: 1 validation error for B pydantic.error_wrappers.ValidationError:使用 FastAPI 的“1”验证错误 - pydantic.error_wrappers.ValidationError: 1 validation error for '' with FastAPI pydantic.error_wrappers.ValidationError: FastAPI - pydantic.error_wrappers.ValidationError: FastAPI pydantic.error_wrappers.ValidationError:值不是有效列表(type=type_error.list) - pydantic.error_wrappers.ValidationError : value is not a valid list (type=type_error.list) 电话号码的 pydantic 自定义数据类型:value_error.missing - pydantic custom data type for phone number : value_error.missing 嵌套 Pydantic Model 使用 FastAPI 返回错误:需要字段(type=value_error.missing) - Nested Pydantic Model return error with FastAPI : field required (type=value_error.missing) Pydantic 嵌套 model 字段抛出 value_error.missing - Pydantic nested model field throws value_error.missing postman api 说“422 无法处理的实体” - value_error.missing - postman api says "422 unprocessable entity" - value_error.missing 如果我进行查询,为什么会得到 {"detail":[{"loc":["path","id"],"msg":"field required","type":"value_error.missing"}]}与参数? - Why am I getting {"detail":[{"loc":["path","id"],"msg":"field required","type":"value_error.missing"}]} if I made query with params? 在后端使用快速 api 和 mongo; 为获取请求获取“value_error.missing” - Using fast api and mongo in backend; getting “value_error.missing” for get request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM