I've been issues setting the "Character Set" and "Collation" for my tables in MySQL created using SQLAlchemy.
In particular, tables were always created using "latin1" for both the "Character Set" and "Collation".
Setting the "charset" in the URL, didn't solve the issue:
f'mysql+pymysql://{_user}:{_pwd}@{_host}:{_port}/{_db}?charset=utf8mb4',
Neither, did create_engine(...,encoding='utf8').
Only by setting the following __table_args__, was the table created with support for UTF-8:
__table_args__ = (
{'mysql_default_charset': 'utf8',
'mysql_collate': 'utf8_bin'}
)
How can I set the above table arguments for all tables in the database without having to specify them explicitly as __table_args__ for each of them?
Full example:
class Match(Base):
__tablename__ = 'stash_match'
id = Column(Integer, primary_key=True, autoincrement=True)
blob_id = Column(String(40))
text = Column(Text, nullable=False)
regex = Column(Text, nullable=False)
blob = relationship('Blob', back_populates='matches')
__table_args__ = (
ForeignKeyConstraint([blob_id], [Blob.id], onupdate='CASCADE', ondelete='CASCADE'),
{'mysql_default_charset': 'utf8',
'mysql_collate': 'utf8_bin'}
)