gpt4 book ai didi

python - pydantic.error_wrappers.ValidationError : 11 validation errors for For Trip type=value_error. 缺失

转载 作者:行者123 更新时间:2023-12-05 04:28:56 25 4
gpt4 key购买 nike

我的 pydantic 模式出现了这个错误,但奇怪的是它正确地生成了对象,并将其发送到 SQLAlchemy 模型,然后它突然对模型中的所有元素抛出错误。

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)

我必须说所有字段都应该有值。而且错误跟踪不引用我代码的任何部分,所以我什至不知道在哪里调试。我是 SQLAlchemy/pydantic 的菜鸟

部分代码

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 模型上的一个错误,它也发生在我身上,我无法修复它,但实际上如果你只是跳过路由中的类型​​检查它工作正常

关于python - pydantic.error_wrappers.ValidationError : 11 validation errors for For Trip type=value_error. 缺失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72476094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com