-
-
Save lefig/07efc3bb65a316cca185b6d66638ac57 to your computer and use it in GitHub Desktop.
Creating a mutable ARRAY data type in sqlalchemy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Something(Base): | |
| __tablename__ = 'yaddayadda' | |
| id = Column(Integer, primary_key=True) | |
| data = Column(MutableList.as_mutable(ARRAY(String(100)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks