Repository: fossasia/open-event-server

Issue:
Cannot save the badge field
```
HINT:  You will need to rewrite or cast the expression.


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/data/app/app/api/helpers/db.py", line 27, in save_to_db
    db.session.commit()
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/orm/scoping.py", line 163, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1046, in commit
    self.transaction.commit()
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 504, in commit
    self._prepare_impl()
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 483, in _prepare_impl
    self.session.flush()
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 2540, in flush
    self._flush(objects)
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 2682, in _flush
    transaction.rollback(_capture_exception=True)
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
    compat.raise_(
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 2642, in _flush
    flush_context.execute()
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
    rec.execute(self)
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 586, in execute
    persistence.save_obj(
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 239, in save_obj
    _emit_insert_statements(
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 1135, in _emit_insert_statements
    result = cached_connections[connection].execute(
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1011, in execute
    return meth(self, multiparams, params)
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/sql/elements.py", line 298, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1124, in _execute_clauseelement
    ret = self._execute_context(
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1316, in _execute_context
    self._handle_dbapi_exception(
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1510, in _handle_dbapi_exception
    util.raise_(
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1276, in _execute_context
    self.dialect.do_execute(
  File "/opt/pysetup/.venv/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 608, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DatatypeMismatch) column "font_weight" is of type integer but expression is of type json[]
LINE 1: ...e', 'Last Name', 'Sample Text', 14, 'Arial', CAST(ARRAY['{"n...
```


Discussion:

Resolving patch:
diff --git a/migrations/versions/rev-2023-08-01-14:10:12-8b5bc48e1d4c_.py b/migrations/versions/rev-2023-08-01-14:10:12-8b5bc48e1d4c_.py
--- a/migrations/versions/rev-2023-08-01-14:10:12-8b5bc48e1d4c_.py
+++ b/migrations/versions/rev-2023-08-01-14:10:12-8b5bc48e1d4c_.py
@@ -17,18 +17,15 @@
 
 def upgrade():
     # ### commands auto generated by Alembic - please adjust! ###
-    op.alter_column('badge_field_forms', 'font_weight',
-               existing_type=sa.TEXT(),
-               type_=postgresql.ARRAY(sa.JSON()),
-               postgresql_using='font_weight::json[]',
-               existing_nullable=True)
+    op.drop_column('badge_field_forms', 'font_weight')
+    op.add_column('badge_field_forms', sa.Column('font_weight',
+                                                 postgresql.ARRAY(sa.JSON()), nullable=True))
     # ### end Alembic commands ###
 
 
 def downgrade():
     # ### commands auto generated by Alembic - please adjust! ###
-    op.alter_column('badge_field_forms', 'font_weight',
-               existing_type=postgresql.ARRAY(sa.JSON()),
-               type_=sa.TEXT(),
-               existing_nullable=True)
+    op.drop_column('badge_field_forms', 'font_weight')
+    op.add_column('badge_field_forms', sa.Column('font_weight',
+                                                 sa.Integer(), nullable=True))
     # ### end Alembic commands ###


Test patch: