Код: Выделить всё
@router.post("/api/calls", status_code=201)
def create_calls(call: CallsCreate):
db: Session = Session()
try:
new_call = Calls(
date=call.date,
call_type=call.call_type,
description=call.description
)
print(new_call)
db.add(new_call)
db.commit()
db.refresh(new_call)
return new_call
except Exception as e:
db.rollback()
raise HTTPException(status_code=400, detail=str(e))
finally:
db.close()
Код: Выделить всё
Error: Bad Request
Response body
{
"detail": "'int' object has no attribute '_sa_instance_state'"
}
Response headers
access-control-allow-credentials: true
access-control-allow-origin: *
content-length: 63
content-type: application/json
date: Mon,16 Sep 2024 09:51:23 GMT
server: uvicorn
the error appeared after I wrote the code for linking tables
`# Определение модели CallType
class CallType(Base):
__tablename__ = "call_types"
id = Column(Integer, primary_key=True, index=True)
fire_ranks = Column(String, unique=True, index=True)
# Связь с Calls
calls = relationship("Calls", back_populates="call_type")
# Определение модели Calls
class Calls(Base):
__tablename__ = "calls"
id = Column(Integer, primary_key=True, index=True)
date = Column(DateTime)
call_type_id = Column(Integer, ForeignKey('call_types.id'))
description = Column(String)
# Связь с CallType
call_type = relationship("CallType", back_populates="calls")
Подробнее здесь: https://stackoverflow.com/questions/789 ... api-how-to