Skip to content

Instantly share code, notes, and snippets.

@WagnerMatos
Forked from kirang89/something.py
Created October 4, 2018 05:49
Show Gist options
  • Save WagnerMatos/6639950852c851a3b582eedfb82bd804 to your computer and use it in GitHub Desktop.
Save WagnerMatos/6639950852c851a3b582eedfb82bd804 to your computer and use it in GitHub Desktop.
Creating a mutable ARRAY data type in sqlalchemy
class Something(Base):
__tablename__ = 'yaddayadda'
id = Column(Integer, primary_key=True)
data = Column(MutableList.as_mutable(ARRAY(String(100))))
from sqlalchemy.ext.mutable import Mutable
from sqlalchemy.dialects.postgresql import ARRAY
class MutableList(Mutable, list):
def append(self, value):
list.append(self, value)
self.changed()
@classmethod
def coerce(cls, key, value):
if not isinstance(value, MutableList):
if isinstance(value, list):
return MutableList(value)
return Mutable.coerce(key, value)
else:
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment