Postgraphql version
3.0.0
Postgres Version
PostgreSQL 9.6.1 on x86_64-pc-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit
Bug description
When postgraphql is started against schema having following things:
Postgraphql will fail to start with following error:
Error: Schema must contain unique named types but contains multiple types named "[NameOfCustomDomain]"
Expected behaviour
I would postgraphql to be able to start in this environment
Steps to reproduce
Run following script to create schema which reproduces the issue:
BEGIN;
CREATE SCHEMA bug_test;
SET SEARCH_PATH TO 'bug_test';
CREATE TABLE my_tbl (
id serial PRIMARY KEY
);
CREATE DOMAIN files_array AS my_tbl[];
CREATE TYPE my_awesome_type AS (
id VARCHAR(255),
files files_array
);
CREATE FUNCTION test2(arg my_awesome_type) RETURNS my_awesome_type AS $$
SELECT NULL::my_awesome_type
$$ LANGUAGE SQL IMMUTABLE;
COMMIT;
Start postgraphql like this:
postgraphql -w -s bug_test -c [POSTGRES_URL]
Observe the error: Error: Schema must contain unique named types but contains multiple types named "FilesArray".
@kenota Thanks for the report; please could you post the results of the introspection query for this schema?
https://github.com/calebmer/postgraphql/blob/master/resources/introspection-query.sql
@benjie Please see attached file. Weirdly enough, quick grep/search shows that it has only 1 entry with name files_array
Indeed; I have to defer to Caleb on this then - sorry!
Additional observation, following schema does not reproduce the issue (note removed argument from the function signature):
BEGIN;
CREATE SCHEMA bug_test;
SET SEARCH_PATH TO 'bug_test';
CREATE TABLE my_tbl (
id serial PRIMARY KEY
);
CREATE DOMAIN files_array AS my_tbl[];
CREATE TYPE my_awesome_type AS (
id VARCHAR(255),
files files_array
);
CREATE FUNCTION test2() RETURNS my_awesome_type AS $$
SELECT NULL::my_awesome_type
$$ LANGUAGE SQL IMMUTABLE;
COMMIT;
Also feel free to change the issue title, I was not sure how to formulate this :)
More investigation: I was able to significantly reduce schema which reproduces the issue. The problem seems to happen also when you just declare function which returns array of records of certain table:
BEGIN;
CREATE SCHEMA bug_test;
SET SEARCH_PATH TO 'bug_test';
CREATE TABLE my_tbl (
id serial PRIMARY KEY
);
CREATE FUNCTION test2() RETURNS my_tbl[] AS $$
SELECT NULL::my_tbl[]
$$ LANGUAGE SQL IMMUTABLE;
COMMIT;
In this schema, error is following:
Error: Schema must contain unique named types but contains multiple types named "MyTbl".
I just spent ~4 hours being confused by that after upgrading to 3.0.0.
It seems like a spelling bug with singular/plural of the table names:
This is working:
```SQL
BEGIN;
CREATE SCHEMA bug_test;
SET SEARCH_PATH TO 'bug_test';
CREATE TABLE my_tbls (
id serial PRIMARY KEY
);
CREATE FUNCTION test2() RETURNS my_tbls[] AS $$
SELECT NULL::my_tbls[]
$$ LANGUAGE SQL IMMUTABLE;
COMMIT;
```
Notice the plurar table name my_tbls !
I just checked version 2.6.0 and it's the same (not working)...
Is this bug in 2.5.1? I believe there was a major types refactor in 2.6.0 and 3.0.0 馃槪
There also might be a couple issues here that have the same effect.
Here's some more details:
2.5.1 fails
2.5.0 fails
...
2.0.0 fails
all with the same
Error: Schema must contain unique named types but contains multiple types named "MyTbl".
I haven't tested before 2.0.0
I think this is quite a major bug because even on the PostGraphQL github front page the very first schema example creates a table with singular name Post which will be affected by this bug
BEGIN;
CREATE SCHEMA bug_test;
SET SEARCH_PATH TO 'bug_test';
CREATE TABLE post (
id serial PRIMARY KEY
);
CREATE FUNCTION test2() RETURNS post[] AS $$
SELECT NULL::post[]
$$ LANGUAGE SQL IMMUTABLE;
COMMIT;
Error: Schema must contain unique named types but contains multiple types named "Post".
Without looking at the code, here's what likely is the problem:
The Post is create as type for the table and another type Post is attempted to be created for the use at the array --> conflict.
However, when the base table is named plural Posts, when attempting to create the list type, It will automagically cut of the trailing plural s, since it's technically a list of post, therefore the second type is Post and there is no conflict.
Again, I haven't seen the code but I imagine the easiest would be to create distinct types for the lists, or why not re-use the existing one's in an array?
I would like to fix it but I'm not a JS guy ^^
I suggest changing the docs to point out that singular table names can cause issues!!!
I truly wonder how no one has noticed this before, given it's been broken for so long... I just decided yesterday to change all my project's table names to singular and upgrade to 3.0.0 to not have to use rowId. Then I thought it's 3.0.0 breaking, but it's actually been broken all the time... what a coincidence.
_Amazing project nevertheless!!!_
VIEW also doesn't work with singular name
ENUM and custom composite types appear to work
so basically the bug just occurs when arrays of TABLE or VIEW (sets?) with plural names are used somewhere (functions, composite types)
(earlier I wrote that VIEW is working, that was wrong)
Thanks for the detailed information @skhro87. I think this is a good first bug for anyone interested in contributing to PostGraphQL and I鈥檇 happily merge a bugfix 馃憤
The bug is probably happening somewhere around here: https://github.com/calebmer/postgraphql/blob/54879f694b3918d256fedf84071703ea4dd5ec10/src/postgraphql/schema/procedures/createPgProcedureQueryGqlFieldEntry.ts
This isn't an issue in v4, I just checked 馃憤
Most helpful comment
I just spent ~4 hours being confused by that after upgrading to
3.0.0.It seems like a spelling bug with singular/plural of the table names:
This is working:
```SQL
BEGIN;
CREATE SCHEMA bug_test;
SET SEARCH_PATH TO 'bug_test';
CREATE TABLE my_tbls (
id serial PRIMARY KEY
);
CREATE FUNCTION test2() RETURNS my_tbls[] AS $$
SELECT NULL::my_tbls[]
$$ LANGUAGE SQL IMMUTABLE;
COMMIT;
```
Notice the plurar table name
my_tbls!