Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

574
Views
Updating a nested Postgresql JSONField with Pydantic, SQLAlchemy, FastAPI

I'm using Postgresql, FastApi, Sqlalchemy, Pydantic I have a model called "Company" one column of the model is postgresql "JSONField" In this model, I plan to keep company addresses as a list.

I managed to add an address to field, but when I want to update or delete any of these addresses, I don't know what to do.

My Pydantic schema

from pydantic import BaseModel, Json
from typing import Optional,List,Dict

class addresses(BaseModel):
    id:int
    name:str

    class Config:
        orm_mode=True

class customers_companies(BaseModel):
    name:str
    #addresses: Json[List[addresses]]
    addresses: List[addresses]
    
    class Config:
        orm_mode=True

Post router

@customers_router.post('/companies')
def create_company(request:schemas.customers_companies, db: Session = Depends(get_db)):
    new_company = models.Customers_Companies(name=request.name,addresses= jsonable_encoder(request.addresses))
    db.add(new_company)
    db.commit()
    db.refresh(new_company)
    return new_company

Get router by ID

@customers_router.get('/companies/{id}', response_model=schemas.customers_companies)
def get_company(id, db: Session = Depends(get_db)):
    company = db.query(models.Customers_Companies).filter(models.Customers_Companies.id==id).first()
    return company

Result of > Get router by ID
for example I want to update only the address with id=1.

{
  "name": "string",
  "addresses": [
    {
      "id": 1,
      "title": "Address 1"
    },
    {
      "id": 2,
      "title": "Address 2"
    }
  ]
}
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!