tortoise / tortoise/tortoise-orm

Unable to create PostgreSQL Function - tortoise.exceptions.OperationalError

Open
#900 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Bug description

I am trying to create db procedures/functions in PostgreSQL, which will be executed via aerich among with other migrations.

As a result I am getting the following error:
tortoise.exceptions.OperationalError: unterminated dollar-quoted string at or near "$$

I have tried to use single quotes instead of dollar quoting, but still getting
OperationalError:
tortoise.exceptions.OperationalError: unterminated quoted string at or near "

The function itself has not a problems, it executes directly into db, via differnt clients or via other migrations tools successfully.

To Reproduce

In order to reproduce, you can create new migration file and define any kind of db function there, simple example here:

-- upgrade --
CREATE FUNCTION sum_n_product(IN x int,IN y int, OUT sum int, OUT prod int) AS $$
BEGIN
    sum := x + y;
    prod := x * y;
END;
$$ LANGUAGE plpgsql;
-- downgrade --
DROP FUNCTION sum_n_product(IN x int,IN y int, OUT sum int, OUT prod int);
Detailed error trace:
  File "/usr/local/lib/python3.8/site-packages/aerich/cli.py", line 119, in upgrade
    await conn.execute_script(upgrade_query)
  File "/usr/local/lib/python3.8/site-packages/tortoise/backends/asyncpg/client.py", line 38, in translate_exceptions_
    raise OperationalError(exc)
tortoise.exceptions.OperationalError: unterminated dollar-quoted string at or near "$$

Env:

aerich==0.5.3
tortoise-orm[asyncpg]~=0.17.7

Is there any way to create procedures, triggers or any other valid raw queries from aerich to avoid errors from Tortoise?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied migration in Aerich, then follow the execute_script call shown in aerich/cli.py and the asyncpg client path in the traceback. Compare handling of the dollar-quoted function with a simple migration; done means the upgrade runs successfully and the downgrade can remove the function without an OperationalError.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.