pg-mem icon indicating copy to clipboard operation
pg-mem copied to clipboard

error: relation "pg_views" does not exist | Using Typeorm

Open KBSchmidt opened this issue 4 years ago • 4 comments

pg-mem fails at this query:

SELECT 'DROP VIEW IF EXISTS "' || schemaname || '"."' || viewname || '" CASCADE;' as "query" FROM "pg_views" WHERE "schemaname" IN ('project', 'project') AND "viewname" NOT IN ('geography_columns', 'geometry_columns', 'raster_columns', 'raster_overviews')

trace:

at new QueryFailedError (/ ...project/packages/backend/src/error/QueryFailedError.ts:9:9) at / ...project/packages/backend/src/driver/postgres/PostgresQueryRunner.ts:178:30 at Timeout._onTimeout (/...project/packages/backend/node_modules/pg-mem/src/adapters.ts:123:42) at listOnTimeout (internal/timers.js:554:17) at processTimers (internal/timers.js:497:7) { data: { error: 'relation "pg_views" does not exist', code: undefined }, location: { start: 0, end: 0 }, query: SELECT 'DROP VIEW IF EXISTS "' || schemaname || '"."' || viewname || '" CASCADE;' as "query" FROM "pg_views" WHERE "schemaname" IN ('project', 'project') AND "viewname" NOT IN ('geography_columns', 'geometry_columns', 'raster_columns', 'raster_overviews'), parameters: []

KBSchmidt avatar Sep 01 '21 15:09 KBSchmidt

Hello all,

Is there any update on this issue? I'm trying to use pg-mem with Typeorm and I'm blocked by this?

Thanks, Matthieu

MatthieuVegreville avatar Jan 20 '22 21:01 MatthieuVegreville

Hi, I thought I answered, sorry. Not yet, it requires a non negible quantity of work that I did not find room for in my calendar yet :/

In the meantime, since it looks like your failing query is an introspection query (meaning that it will return nothing on a fresh database), you might want to try this workaround, but with your query.

(tldr: intercept the incriminated query, and prevent its execution by returning an empty array instead)

oguimbal avatar Jan 21 '22 08:01 oguimbal

Thank you a lot, we actually succeeded in getting past this step thanks to the workaround. For those who could find it interesting, here is what our test spin up looks like with Typeorm:

 const db = newDb();

db.public.registerFunction({
    implementation: () => 'test',
    name: 'current_database',
});

db.public.interceptQueries(queryText => {
    if (queryText.search(/(pg_views|pg_matviews|pg_tables|pg_enum)/g) > -1) {
    return [];
    }
    return null;
});

db.public.registerFunction({
    name: 'jsonb_typeof',
    args: [DataType.jsonb],
    returns: DataType.text,
    implementation: x => (x ? x.constructor.name : null),
});

const connection = await db.adapters.createTypeormConnection({
    type: 'postgres',
    entities: [],
});

We are now stuck with another issue with the jsonb question mark operator. Would gladly welcome your insights, I opened a new issue for this.

MatthieuVegreville avatar Jan 22 '22 08:01 MatthieuVegreville

Nice! Thanks.

A detail that you might want to know: Your custom functions implementations will never be called with a null argument - the function call will be evaluated as null if one of its argument is null - that is a pretty common use case with PG, so I thought it would be better to make it the default behaviour to avoid unnecessary nullrefs & useless code.

So the x ? x.constructor.name : null null-check is unnecessary.

(if you function implementation must accept null arguments, use the allowNullArguments: true option when calling registerFunction()).

oguimbal avatar Jan 22 '22 19:01 oguimbal