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"
Advertisement
Answer
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