Mrmar
12/08/2025, 4:51 PMMarvin
12/08/2025, 4:51 PMMarvin
12/08/2025, 4:52 PMpip install "prefect[server]"
- Register the SqlAlchemyConnector block type and create a test block inside the harness
- The harness starts a fresh, empty API/database; block types and instances won’t exist there unless you register/create them within the with prefect_test_harness(): block
Example pytest pattern:
import pytest
from prefect.testing.utilities import prefect_test_harness
from prefect_sqlalchemy import SqlAlchemyConnector, register_blocks
# Suppose your code under test does: SqlAlchemyConnector.load("<name>")
def test_my_fn():
with prefect_test_harness():
# Make the block type available in this fresh test DB
register_blocks() # or at least import SqlAlchemyConnector so it registers
# Create a temporary connector block for the test
connector = SqlAlchemyConnector(connection_url="sqlite+aiosqlite://")
connector.save("test-conn", overwrite=True)
# Call the function that loads/uses the block
result = my_function_that_loads_block("test-conn")
# Your assertion(s) here
assert isinstance(result, list)
If you still see “NameError: name PrefectDBInterface is not defined,” it’s typically one of:
- Missing server extras for local API: fix with pip install "prefect[server]".
- Mixed/old install (bits of Prefect 2.x lingering): recreate the venv and install Prefect 3.x cleanly, plus prefect[server].
- Running the harness but trying to load a block that only exists in your real workspace (not the test DB): create it inside the harness as shown above.
If you share:
- Your Prefect and prefect-sqlalchemy versions
- How you invoke prefect_test_harness in the test
- The exact stack trace segment where the NameError appears
…I can pinpoint which of the above is the culprit and tailor the fix.Mrmar
12/08/2025, 5:27 PMMrmar
12/08/2025, 6:42 PMMarvin
12/08/2025, 6:42 PMMarvin
12/08/2025, 6:44 PMfrom prefect import flow, get_run_logger
from prefect_sqlalchemy import SqlAlchemyConnector
from sqlalchemy import text
@flow
def main():
logger = get_run_logger()
# Tip: source this URL from an env var or secret to avoid committing creds
connector = SqlAlchemyConnector(
connection_info="<postgresql+psycopg://user:pass@host:5432/dbname>"
)
with connector.get_connection() as conn:
rows = conn.execute(text("SELECT 1")).all()
<http://logger.info|logger.info>(f"Result: {rows}")
if __name__ == "__main__":
main()
Optionally, save/load as a Block:
# one-time setup
SqlAlchemyConnector(connection_info="postgresql+psycopg://...").save("prod-db", overwrite=True)
# later in flows
connector = SqlAlchemyConnector.load("prod-db")
with connector.get_connection() as conn:
...
Docs:
- SqlAlchemy collection: prefect-sqlalchemy docs
- Blocks in Prefect 3: Blocks concept
If you can share your database type and what you’re trying to run (or any error message), I can tailor the exact setup.