I want to set the default value of my SQLAlchemy postgres JSON column to an empty dictionary.

from sqlalchemy.dialects.postgresql import JSON

info = Column(JSON, default='{}')
info = Column(JSON, default={})

Neither of these work.

share|improve this question
1  
It's probably because the same dict instance is being used across all instances of your model. Try default=lambda: {}. – univerio Aug 15 at 20:22
    
what goes wrong? – shane Aug 15 at 20:41
    
Thanks univerio that works. You can submit an answer and I can accept if you'd like. Otherwise I'll write my own answer. – Allen Lin Aug 15 at 21:17
up vote 0 down vote accepted

Using default=lambda: {} works. Credit goes to univerio in the comments.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.