pydantic.error_wrappers.ValidationError: 11 validation errors for For Trip type=value_error.missing
Question:
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.
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
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"
Answers:
It seems like a bug on the pydantic model, it happened to me as well, and i was not able to fix it, but indeed if you just skip the type check in the route it works fine
It seems like there is a conflict in your schema and create_trip
function. Have you checked whether you are passing the correct param to your schema? You have defined most of the fields as not nullable, and you are passing None as an alternative value in db.add()
command.
I had a similar problem with my code, and I figured that naming and type convention between schema and server.py. After matching the field names and type in both file, I resolved the error of the field required (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.
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.
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
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"
It seems like a bug on the pydantic model, it happened to me as well, and i was not able to fix it, but indeed if you just skip the type check in the route it works fine
It seems like there is a conflict in your schema and create_trip
function. Have you checked whether you are passing the correct param to your schema? You have defined most of the fields as not nullable, and you are passing None as an alternative value in db.add()
command.
I had a similar problem with my code, and I figured that naming and type convention between schema and server.py. After matching the field names and type in both file, I resolved the error of the field required (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.