SQLAlchemy Issue

Running into the below issue when creating a dev instance on Windows 10. I get a constant SQLAlchemy internal error. I have removed .data folder, rebuilt images, changed the CRLF on Dockerfile, docker-compose.yaml, and docker-entrypoint.sh butcannot find the cause of this issue. Maybe one of you have seen it before. Thanks in advance.

PS E:\Programming\Python\CTFd> docker-compose up
Starting ctfd_cache_1 ... done
Starting ctfd_db_1    ... done
Starting ctfd_ctfd_1  ... done
Attaching to ctfd_cache_1, ctfd_db_1, ctfd_ctfd_1
cache_1  | 1:C 03 Mar 23:56:14.265 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
cache_1  | 1:C 03 Mar 23:56:14.265 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=1, just started
cache_1  | 1:C 03 Mar 23:56:14.265 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
cache_1  | 1:M 03 Mar 23:56:14.266 * Running mode=standalone, port=6379.
cache_1  | 1:M 03 Mar 23:56:14.266 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
cache_1  | 1:M 03 Mar 23:56:14.266 # Server initialized
cache_1  | 1:M 03 Mar 23:56:14.266 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run 
the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
cache_1  | 1:M 03 Mar 23:56:14.267 * DB loaded from disk: 0.001 seconds
cache_1  | 1:M 03 Mar 23:56:14.267 * Ready to accept connections
ctfd_1   | Waiting for db: to be ready
db_1     | 2020-03-03 23:56:14+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.4.12+maria~bionic started.
db_1     | 2020-03-03 23:56:14+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
db_1     | 2020-03-03 23:56:14+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 1:10.4.12+maria~bionic started.
db_1     | 2020-03-03 23:56:15 0 [Note] mysqld (mysqld 10.4.12-MariaDB-1:10.4.12+maria~bionic) starting as process 1 ...
db_1     | 2020-03-03 23:56:15 0 [Note] InnoDB: Using Linux native AIO
db_1     | 2020-03-03 23:56:15 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1     | 2020-03-03 23:56:15 0 [Note] InnoDB: Uses event mutexes
db_1     | 2020-03-03 23:56:15 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
db_1     | 2020-03-03 23:56:15 0 [Note] InnoDB: Number of pools: 1
db_1     | 2020-03-03 23:56:15 0 [Note] InnoDB: Using SSE2 crc32 instructions
db_1     | 2020-03-03 23:56:15 0 [Note] mysqld: O_TMPFILE is not supported on /tmp (disabling future attempts)
db_1     | 2020-03-03 23:56:15 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
db_1     | 2020-03-03 23:56:15 0 [Note] InnoDB: Completed initialization of buffer pool
db_1     | 2020-03-03 23:56:15 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
db_1     | 2020-03-03 23:56:15 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
db_1     | 2020-03-03 23:56:15 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1     | 2020-03-03 23:56:15 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1     | 2020-03-03 23:56:16 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1     | 2020-03-03 23:56:16 0 [Note] InnoDB: 10.4.12 started; log sequence number 246995; transaction id 163
db_1     | 2020-03-03 23:56:16 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1     | 2020-03-03 23:56:16 0 [Note] Server socket created on IP: '::'.
db_1     | 2020-03-03 23:56:16 0 [Warning] 'proxies_priv' entry '@% root@5edf77d0f068' ignored in --skip-name-resolve mode.
db_1     | 2020-03-03 23:56:16 0 [Note] mysqld: ready for connections.
db_1     | Version: '10.4.12-MariaDB-1:10.4.12+maria~bionic'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution
db_1     | 2020-03-03 23:56:16 0 [Note] InnoDB: Buffer pool(s) load completed at 200303 23:56:16
ctfd_1   | ..db is ready
ctfd_1   | Traceback (most recent call last):
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1246, in _execute_context
ctfd_1   |     cursor, statement, parameters, context
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 581, in do_execute
ctfd_1   |     cursor.execute(statement, parameters)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
ctfd_1   |     result = self._query(query)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
ctfd_1   |     conn.query(q)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 517, in query
ctfd_1   |     self._affected_rows = self._read_query_result(unbuffered=unbuffered)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 732, in _read_query_result
ctfd_1   |     result.read()
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 1075, in read
ctfd_1   |     first_packet = self.connection._read_packet()
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 684, in _read_packet
ctfd_1   |     packet.check_error()
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/pymysql/protocol.py", line 220, in check_error
ctfd_1   |     err.raise_mysql_exception(self._data)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
ctfd_1   |     raise errorclass(errno, errval)
ctfd_1   | pymysql.err.InternalError: (1060, "Duplicate column name 'captain_id'")
ctfd_1   |
ctfd_1   | The above exception was the direct cause of the following exception:
ctfd_1   | 
ctfd_1   | Traceback (most recent call last):
ctfd_1   |   File "manage.py", line 9, in <module>
ctfd_1   |     app = create_app()
ctfd_1   |   File "/opt/CTFd/CTFd/__init__.py", line 164, in create_app
ctfd_1   |     upgrade()
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/flask_migrate/__init__.py", line 95, in wrapped
ctfd_1   |     f(*args, **kwargs)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/flask_migrate/__init__.py", line 280, in upgrade
ctfd_1   |     command.upgrade(config, revision, sql=sql, tag=tag)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/alembic/command.py", line 298, in upgrade
ctfd_1   |     script.run_env()
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/alembic/script/base.py", line 489, in run_env
ctfd_1   |     util.load_python_file(self.dir, "env.py")
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/alembic/util/pyfiles.py", line 98, in load_python_file
ctfd_1   |     module = load_module_py(module_id, path)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/alembic/util/compat.py", line 184, in load_module_py
ctfd_1   |     spec.loader.exec_module(module)
ctfd_1   |   File "<frozen importlib._bootstrap_external>", line 728, in exec_module
ctfd_1   |   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ctfd_1   |   File "migrations/env.py", line 99, in <module>
ctfd_1   |     run_migrations_online()
ctfd_1   |   File "migrations/env.py", line 91, in run_migrations_online
ctfd_1   |     context.run_migrations()
ctfd_1   |   File "<string>", line 8, in run_migrations
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/alembic/runtime/environment.py", line 846, in run_migrations
ctfd_1   |     self.get_context().run_migrations(**kw)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/alembic/runtime/migration.py", line 518, in run_migrations
ctfd_1   |     step.migration_fn(**kw)
ctfd_1   |   File "/opt/CTFd/migrations/versions/b5551cd26764_add_captain_column_to_teams.py", line 27, in upgrade
ctfd_1   |     op.add_column("teams", sa.Column("captain_id", sa.Integer(), nullable=True))
ctfd_1   |   File "<string>", line 8, in add_column
ctfd_1   |   File "<string>", line 3, in add_column
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/alembic/operations/ops.py", line 1927, in add_column
ctfd_1   |     return operations.invoke(op)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/alembic/operations/base.py", line 374, in invoke
ctfd_1   |     return fn(self, operation)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/alembic/operations/toimpl.py", line 132, in add_column
ctfd_1   |     operations.impl.add_column(table_name, column, schema=schema, **kw)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/alembic/ddl/impl.py", line 237, in add_column
ctfd_1   |     self._exec(base.AddColumn(table_name, column, schema=schema))
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/alembic/ddl/impl.py", line 140, in _exec
ctfd_1   |     return conn.execute(construct, *multiparams, **params)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 982, in execute
ctfd_1   |     return meth(self, multiparams, params)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/sql/ddl.py", line 72, in _execute_on_connection
ctfd_1   |     return connection._execute_ddl(self, multiparams, params)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1044, in _execute_ddl
ctfd_1   |     compiled,
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1250, in _execute_context
ctfd_1   |     e, statement, parameters, cursor, context
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1476, in _handle_dbapi_exception
ctfd_1   |     util.raise_from_cause(sqlalchemy_exception, exc_info)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
ctfd_1   |     reraise(type(exception), exception, tb=exc_tb, cause=cause)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 152, in reraise
ctfd_1   |     raise value.with_traceback(tb)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 1246, in _execute_context
ctfd_1   |     cursor, statement, parameters, context
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 581, in do_execute
ctfd_1   |     cursor.execute(statement, parameters)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
ctfd_1   |     result = self._query(query)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
ctfd_1   |     conn.query(q)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 517, in query
ctfd_1   |     self._affected_rows = self._read_query_result(unbuffered=unbuffered)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 732, in _read_query_result
ctfd_1   |     result.read()
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 1075, in read
ctfd_1   |     first_packet = self.connection._read_packet()
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 684, in _read_packet
ctfd_1   |     packet.check_error()
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/pymysql/protocol.py", line 220, in check_error
ctfd_1   |     err.raise_mysql_exception(self._data)
ctfd_1   |   File "/usr/local/lib/python3.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
ctfd_1   |     raise errorclass(errno, errval)
ctfd_1   | sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1060, "Duplicate column name 'captain_id'")
ctfd_1   | [SQL: ALTER TABLE teams ADD COLUMN captain_id INTEGER]
ctfd_1   | (Background on this error at: http://sqlalche.me/e/2j85)

I had this issue when using the master branch. I switched to the latest release and created a new branch from that and it worked fine.

@jimender2 Thanks, I’ll give this a shot. Well… I pulled the latest release (2.3.1), deleted all containers, and previous versions installed… No dice. I ran docker-compose.exe up and everything compiled/built and same error. The main error I get is related to pymysql.err.InternalError: (1060, "Duplicate column name 'captain_id'") even after blowing away the db and starting new.

Everything works as intended on Mac and Linux. CTFd isn’t a fan of Windows I guess. ¯\_(ツ)_/¯