Browse Source

Set JSONB columns to use none_as_null

By default, sqlalchemy will set the JSON value of null (the string
"null") in JSONB columns if you set them to None. I want it to set the
actual column to NULL, so they need to be defined with the none_as_null
argument set to True like this.
merge-requests/37/head
Deimos 6 years ago
parent
commit
f16788b039
  1. 2
      tildes/tildes/models/log/log.py
  2. 2
      tildes/tildes/models/scraper/scraper_result.py
  3. 4
      tildes/tildes/models/topic/topic.py
  4. 2
      tildes/tildes/models/user/user.py

2
tildes/tildes/models/log/log.py

@ -57,7 +57,7 @@ class BaseLog:
@declared_attr @declared_attr
def info(self) -> Column: def info(self) -> Column:
"""Return the info column.""" """Return the info column."""
return Column(MutableDict.as_mutable(JSONB))
return Column(MutableDict.as_mutable(JSONB(none_as_null=True)))
@declared_attr @declared_attr
def user(self) -> Any: def user(self) -> Any:

2
tildes/tildes/models/scraper/scraper_result.py

@ -28,7 +28,7 @@ class ScraperResult(DatabaseModel):
index=True, index=True,
server_default=text("NOW()"), server_default=text("NOW()"),
) )
data: Any = Column(JSONB)
data: Any = Column(JSONB(none_as_null=True))
def __init__(self, url: str, scraper_type: ScraperType, data: Any) -> None: def __init__(self, url: str, scraper_type: ScraperType, data: Any) -> None:
"""Create a new ScraperResult.""" """Create a new ScraperResult."""

4
tildes/tildes/models/topic/topic.py

@ -106,7 +106,9 @@ class Topic(DatabaseModel):
_markdown: Optional[str] = deferred(Column("markdown", Text)) _markdown: Optional[str] = deferred(Column("markdown", Text))
rendered_html: Optional[str] = Column(Text) rendered_html: Optional[str] = Column(Text)
link: Optional[str] = Column(Text) link: Optional[str] = Column(Text)
content_metadata: Dict[str, Any] = Column(MutableDict.as_mutable(JSONB))
content_metadata: Dict[str, Any] = Column(
MutableDict.as_mutable(JSONB(none_as_null=True))
)
num_comments: int = Column(Integer, nullable=False, server_default="0", index=True) num_comments: int = Column(Integer, nullable=False, server_default="0", index=True)
num_votes: int = Column(Integer, nullable=False, server_default="0", index=True) num_votes: int = Column(Integer, nullable=False, server_default="0", index=True)
_tags: List[Ltree] = Column( _tags: List[Ltree] = Column(

2
tildes/tildes/models/user/user.py

@ -94,7 +94,7 @@ class User(DatabaseModel):
open_new_tab_text: bool = Column(Boolean, nullable=False, server_default="false") open_new_tab_text: bool = Column(Boolean, nullable=False, server_default="false")
theme_default: str = Column(Text) theme_default: str = Column(Text)
is_banned: bool = Column(Boolean, nullable=False, server_default="false") is_banned: bool = Column(Boolean, nullable=False, server_default="false")
permissions: Any = Column(JSONB)
permissions: Any = Column(JSONB(none_as_null=True))
home_default_order: Optional[TopicSortOption] = Column(ENUM(TopicSortOption)) home_default_order: Optional[TopicSortOption] = Column(ENUM(TopicSortOption))
home_default_period: Optional[str] = Column(Text) home_default_period: Optional[str] = Column(Text)
_filtered_topic_tags: List[Ltree] = Column( _filtered_topic_tags: List[Ltree] = Column(

Loading…
Cancel
Save