Automation library · Content

Generate & test SQL code with OpenRouter AI and PostgreSQL

Started by someone opening a chat with it, and works across MySQL and Postgres. It puts OpenAI and OpenRouter behind a retrieval step, so answers are drawn from documents you supply rather than from the model alone. 33 steps, 9 branches: large, and worth reading before you trust it with anything live.

LargeCalls a model: OpenAI, OpenRouterRetrieval

33 stepsLarge: read it before trusting it with anything live
9Branches: places the flow splits
0Loops or waits
2Services it connects to

The flow

Starts it 1Model 6Logic 24Service 2

The steps in the order they run, from what starts it.

  1. 1When chat message receivedChat triggerStarts it
  2. 2Simple MemoryMemory Buffer WindowModel
  3. 3OpenRouter Chat ModelLm Chat Open RouterModel
  4. 4localVariablesSetLogic
  5. 5OpenRouterAgentAgentModel
  6. 6IfOpenAIIfLogic
  7. 7isExecutableIfLogic
  8. 8AgentNameSetLogic
  9. 9setOutputByProviderSetLogic
  10. 10wordsForUser1SetLogic
  11. 11getAssistantsListOpen AiModel
  12. 12IfErrorIfLogic
  13. 13IfIfLogic
  14. 14AutoErrorFixingIfLogic
  15. 15SwitchSwitchLogic
  16. 16isAssistantExistsCodeCodeLogic
  17. 17issueOnOpenAiSideSetLogic
  18. 18IsMaxAutoErrorReachedIfLogic
  19. 19askUserHowToHandleErrorSetLogic
  20. 20Execute_AI_resultPostgresService
  21. 21Execute a SQL queryMy SqlService
  22. 22isAssistantExistsIfLogic
  23. 23collectErrorLoopDetails1CodeLogic
  24. 24mergeExecutionsResultCodeLogic
  25. 25GenerateErrorPromptCodeLogic
  26. 26assistantSetLogic
  27. 27createOpenAiAssistantOpen AiModel
  28. 28maxAutoErrorLimitReachedSetLogic
  29. 29collectErrorLoopDetailsCodeLogic
  30. 30isOpenAIIfLogic
  31. 31OpenAIMainBrainOpen AiModel
  32. 32executedSQLQuerySetLogic
  33. 33setThreadIdSetLogic

How it's built

Starts on
Someone opening a chat with it
Steps
33, with 9 branches
Size
Large: read it before trusting it with anything live
Connects to
MySQL, Postgres
Model
OpenAI, OpenRouter
Retrieval
Answers are drawn from documents you supply, not from the model alone

Questions

What does it need to run?

An n8n account, plus your own MySQL and Postgres accounts, and a key for OpenAI and OpenRouter. You connect your own accounts after importing it; none are included.

What starts it?

It starts on someone opening a chat with it.

Does it use AI?

Yes. It calls OpenAI and OpenRouter. Its answers draw on documents you give it, not on the model alone. Model calls are billed by the provider, not by n8n.

How long does it take to set up?

Take your time. At 33 steps it is large: read it before trusting it with anything live.

How do I import it?

Copy the workflow, open a new workflow in n8n and paste. The steps appear on the canvas, ready for your own accounts.

Before you run it

Nothing here has been tested by us. It is somebody's working automation, read and described from its own steps. Treat the connections it wants as a list of accounts it will touch, and read the 33 steps before you point it at anything that matters.