pydantic.error_wrappers.ValidationError: for Trip



我得到这个错误与我的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模型上的一个bug,它也发生在我身上,我无法修复它,但如果你只是跳过路由中的类型检查,它就会工作得很好

您的模式和create_trip函数似乎存在冲突。您是否检查了是否向模式传递了正确的参数?您已经将大多数字段定义为不可空的,并且您在db.add()命令中传递None作为替代值。

我的代码也有类似的问题,我认为schema和server.py之间的命名和类型约定。在匹配了两个文件中的字段名和类型之后,我解决了所需字段的错误(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

请检查return语句,我有类似的问题,并通过纠正我的return语句解决了这个问题。我在create_trip函数中看到返回语句缺失或错误。

最新更新