Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema qualifier for enums #3307

Open
parm-rm opened this issue Jun 25, 2019 · 6 comments
Open

Schema qualifier for enums #3307

parm-rm opened this issue Jun 25, 2019 · 6 comments

Comments

@parm-rm
Copy link

@parm-rm parm-rm commented Jun 25, 2019

Environment

Knex version: 0.17.6
Database + version: PostgreSQL 11.4
OS: Manjaro Linux 18.0.4

Feature discussion / request

While creating a table with an explicit schema and I'm looking to use a native enum as one of the columns. I haven't found any documentation about how to qualify an enum declaration with schema name.

I've tried the following

table.enum('bazz_type', [ 'foo', 'bar' ], {
	useNative : true,
	enumName  : 'other_schema.bazz_types',
});

but it lead to creation of other_schema.bazz_types under public schema

Debugging output:
'create type ' +
'"other_schema.bazz_types" ' +
"as enum ('foo', 'bar')

One possible solution could be to add a schema string option under EnumOptions.

@elhigu
Copy link
Member

@elhigu elhigu commented Jun 25, 2019

Yep, looks like a bug that enum type creation does not respect withSchema.

https://runkit.com/embed/e0rde4cgbloq

for a workaround this should work though:

knex.schema.withSchema('foo').table('table', t => {
  t.enum('enum', ['foo', 'bar'], { useNative: true, enumName: knex.ref('foo.enu_type') });
})

Loading

@elhigu
Copy link
Member

@elhigu elhigu commented Jun 25, 2019

Do you want table and enum type to live in different schemas?

Loading

@parm-rm
Copy link
Author

@parm-rm parm-rm commented Jun 26, 2019

Do you want table and enum type to live in different schemas?

No, just wanted it to respect table schema context. Having a schema override for enums too is "nice-to-have".

Loading

@elhigu
Copy link
Member

@elhigu elhigu commented Jun 26, 2019

Ok, I'll label this as a bug then :)

Loading

@parm-rm
Copy link
Author

@parm-rm parm-rm commented Jun 27, 2019

Yep, looks like a bug that enum type creation does not respect withSchema.

https://runkit.com/embed/e0rde4cgbloq

for a workaround this should work though:

knex.schema.withSchema('foo').table('table', t => {
  t.enum('enum', ['foo', 'bar'], { useNative: true, enumName: knex.ref('foo.enu_type') });
})

That workaround doesn't seem to work. Instead of generating the following SQL

create type "foo"."enu_type" as enum ('foo', 'bar')

it generates

create type ""foo"."enu_type"" as enum ('foo', 'bar')

which is invalid and leads to zero-length delimited identifier at or near """" error.

Loading

@elhigu
Copy link
Member

@elhigu elhigu commented Jun 27, 2019

Right I missed that... so looks like there is no nice workaround for that, but one must use knex.schema.raw() to create type manually... That double quoting problem seems to be also when type name is used when creating enum column :(

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants