SQLAlchemy: JSON vs. PickleType vs. raw string for serialised data

Albert-Jan Roskam sjeik_appie at hotmail.com
Mon Feb 28 12:13:32 EST 2022


   On Feb 28, 2022 10:11, Loris Bennett <loris.bennett at fu-berlin.de> wrote:

     Hi,

     I have an SQLAlchemy class for an event:

       class UserEvent(Base):
           __tablename__ = "user_events"

           id = Column('id', Integer, primary_key=True)
           date = Column('date', Date, nullable=False)
           uid = Column('gid', String(64), ForeignKey('users.uid'),
     nullable=False)
           info = ??

     The event may have arbitrary, but dict-like data associated with it,
     which I want to add in the field 'info'.  This data never needs to be
     modified, once the event has been inserted into the DB.

     What type should the info field have?  JSON, PickleType, String, or
     something else?

     I couldn't find any really reliable sounding information about the
     relative
     pros and cons, apart from a Reddit thread claiming that pickled dicts
     are larger than dicts converted to JSON or String.

     Cheers,

     Loris

   ====
   I think you need a
   BLOB. https://docs.sqlalchemy.org/en/14/core/type_basics.html#sqlalchemy.types.LargeBinary


More information about the Python-list mailing list