I was using prefect 2.3.1 and now I'm trying to up...
# ask-community
m
I was using prefect 2.3.1 and now I'm trying to upgrade to the latest version 2.10.8. While trying to start the server with the
prefect server start
command, this error is popping up. How can I fix this without having to clear or use a new database?
Copy code
File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 442, in _prepare_and_execute
    self._rows = await prepared_stmt.fetch(*parameters)
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/asyncpg/prepared_stmt.py", line 176, in fetch
    data = await self.__bind_execute(args, 0, timeout)
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/asyncpg/prepared_stmt.py", line 241, in __bind_execute
    data, status, _ = await self.__do_execute(
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/asyncpg/prepared_stmt.py", line 230, in __do_execute
    return await executor(protocol)
  File "asyncpg/protocol/protocol.pyx", line 201, in bind_execute
asyncpg.exceptions.CardinalityViolationError: more than one row returned by a subquery used as an expression

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

Traceback (most recent call last):
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1900, in _execute_context
    self.dialect.do_execute(
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
    cursor.execute(statement, parameters)
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 479, in execute
    self._adapt_connection.await_(
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 68, in await_only
    return current.driver.switch(awaitable)
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 121, in greenlet_spawn
    value = await result
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 454, in _prepare_and_execute
    self._handle_exception(error)
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 389, in _handle_exception
    self._adapt_connection._handle_exception(error)
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 682, in _handle_exception
    raise translated_error from error
sqlalchemy.dialects.postgresql.asyncpg.AsyncAdapt_asyncpg_dbapi.Error: <class 'asyncpg.exceptions.CardinalityViolationError'>: more than one row returned by a subquery used as an expression

  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1900, in _execute_context
    self.dialect.do_execute(
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
    cursor.execute(statement, parameters)
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 479, in execute
    self._adapt_connection.await_(
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 68, in await_only
    return current.driver.switch(awaitable)
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/util/_concurrency_py3k.py", line 121, in greenlet_spawn
    value = await result
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 454, in _prepare_and_execute
    self._handle_exception(error)
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 389, in _handle_exception
    self._adapt_connection._handle_exception(error)
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 682, in _handle_exception
    raise translated_error from error
sqlalchemy.exc.DBAPIError: (sqlalchemy.dialects.postgresql.asyncpg.Error) <class 'asyncpg.exceptions.CardinalityViolationError'>: more than one row returned by a subquery used as an expression
[SQL: 
            UPDATE task_run_state
            SET result_artifact_id = (SELECT id FROM artifact WHERE task_run_state.id = task_run_state_id)
            WHERE task_run_state.id in (SELECT id FROM task_run_state WHERE (has_data IS TRUE) AND (result_artifact_id IS NULL) LIMIT 500);
        ]
(Background on this error at: <https://sqlalche.me/e/14/dbapi>)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/starlette/datastructures.py", line 702, in __getattr__
    return self._state[key]
KeyError: 'services'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/starlette/routing.py", line 677, in lifespan
    async with self.lifespan_context(app) as maybe_state:
  File "/usr/lib/python3.8/contextlib.py", line 171, in __aenter__
    return await self.gen.__anext__()
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/prefect/server/api/server.py", line 486, in lifespan
    await stop_services()
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/prefect/server/api/server.py", line 468, in stop_services
    if app.state.services:
  File "/home/malavika/.virtualenvs/healthgraph/lib/python3.8/site-packages/starlette/datastructures.py", line 705, in __getattr__
    raise AttributeError(message.format(self.__class__.__name__, key))
AttributeError: 'State' object has no attribute 'services'

01:04:07.255 | ERROR   | uvicorn.error - Application startup failed. Exiting.
d
I had this as well. The weird part for me was that I was having issues connecting on the machine I ran the upgrade command. I used some other machine to run server and it was fine. Maybe try restart the database server?
a
You have to migrate the database https://docs.prefect.io/latest/host/#migrations
d
I believe I tried migration and then it timed out. The next time I tried it had the
'State' object has no attribute 'services'
issue.
a
Do you have a backup of the database?
d
I just manually droped the database to fix it. I am still testing Prefect extensively before moving it to prod.