tortoise / aerich

A database migrations tool for TortoiseORM, ready to production.
https://github.com/tortoise/aerich
Apache License 2.0
823 stars 93 forks source link

Don't create errors in the database logs #144

Open Olegt0rr opened 3 years ago

Olegt0rr commented 3 years ago

Problem

On the init aerich trying to get aerich table and creates errors in the db log:

2021-03-29 20:29:26.345 UTC [153] ERROR:  relation "aerich" does not exist at character 15
2021-03-29 20:29:26.345 UTC [153] STATEMENT:  SELECT 1 FROM "aerich" WHERE "version"='0_20210325003854_init.sql' AND "app"='models' LIMIT 1
2021-03-29 21:00:02.812 UTC [229] ERROR:  relation "aerich" does not exist at character 44
2021-03-29 21:00:02.812 UTC [229] STATEMENT:  SELECT "id","content","app","version" FROM "aerich" WHERE "app"='models' ORDER BY "id" DESC LIMIT 1

Solution

You should avoid creating errors by checking table aerich exists first.

For example:

SELECT EXISTS (
   SELECT FROM pg_tables
   WHERE  schemaname = 'schema_name'
   AND    tablename  = 'table_name'
   );

More info:

https://stackoverflow.com/questions/20582500/how-to-check-if-a-table-exists-in-a-given-schema