# Buggy code
def _on_table_create(self, target, bind, checkfirst=False, **kw):
    if (
        checkfirst
        or (not self.metadata and not kw.get("_is_metadata_operation", False))
        and not self._check_for_name_in_memos(checkfirst, kw)
    ):
        self.create(bind=bind, checkfirst=checkfirst)


# Traceback (sqlalchemy.exc.ProgrammingError)
Traceback (most recent call last):
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1277, in _execute_context
self.dialect.do_execute(
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 593, in do_execute
cursor.execute(statement, parameters)
psycopg2.errors.DuplicateObject: type "mood" already exists


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/Users/robert_muil/Library/Application Support/JetBrains/PyCharm2020.2/scratches/scratch_9.py", line 51, in <module>
raise ex
File "/Users/robert_muil/Library/Application Support/JetBrains/PyCharm2020.2/scratches/scratch_9.py", line 48, in <module>
Base.metadata.create_all(bind=conn)
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/sql/schema.py", line 4546, in create_all
bind._run_visitor(
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1657, in _run_visitor
visitorcallable(self.dialect, self, **kwargs).traverse_single(element)
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/sql/visitors.py", line 144, in traverse_single
return meth(obj, **kw)
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/sql/ddl.py", line 783, in visit_metadata
self.traverse_single(
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/sql/visitors.py", line 144, in traverse_single
return meth(obj, **kw)
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/sql/ddl.py", line 811, in visit_table
table.dispatch.before_create(
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/event/attr.py", line 322, in __call__
fn(*args, **kw)
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 645, in __call__
return getattr(self.target, self.name)(*arg, **kw)
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/base.py", line 1575, in _on_table_create
self.create(bind=bind, checkfirst=checkfirst)
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/base.py", line 1510, in create
bind.execute(CreateEnumType(self))
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1014, in execute
return meth(self, multiparams, params)
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/sql/ddl.py", line 72, in _execute_on_connection
return connection._execute_ddl(self, multiparams, params)
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1071, in _execute_ddl
ret = self._execute_context(
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1317, in _execute_context
self._handle_dbapi_exception(
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1511, in _handle_dbapi_exception
util.raise_(
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1277, in _execute_context
self.dialect.do_execute(
File "/usr/local/anaconda3/envs/datacat/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 593, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DuplicateObject) type "mood" already exists

[SQL: CREATE TYPE minimal_failing_example.mood AS ENUM ('sad', 'ok', 'happy')]
(Background on this error at: http://sqlalche.me/e/13/f405)

# Fixed code
def _on_table_create(self, target, bind, checkfirst=False, **kw):
    if (
        checkfirst
        or (not self.metadata and not kw.get("_is_metadata_operation", False))
    ) and not self._check_for_name_in_memos(checkfirst, kw):
        self.create(bind=bind, checkfirst=checkfirst)