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

Comments

@parm-rm
Copy link

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

This comment has been minimized.

Copy link
Collaborator

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') });
})
@elhigu

This comment has been minimized.

Copy link
Collaborator

commented Jun 25, 2019

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

@parm-rm

This comment has been minimized.

Copy link
Author

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".

@elhigu

This comment has been minimized.

Copy link
Collaborator

commented Jun 26, 2019

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

@parm-rm

This comment has been minimized.

Copy link
Author

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.

@elhigu

This comment has been minimized.

Copy link
Collaborator

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 :(

Saka7 added a commit to Saka7/knex that referenced this issue Aug 16, 2019
Saka7 added a commit to Saka7/knex that referenced this issue Aug 16, 2019
Saka7 added a commit to Saka7/knex that referenced this issue Aug 16, 2019
Saka7 added a commit to Saka7/knex that referenced this issue Aug 24, 2019
kibertoad added a commit that referenced this issue Aug 24, 2019
ivanovych666 added a commit to ivanovych666/knex that referenced this issue Aug 25, 2019
ivanovych666 added a commit to ivanovych666/knex that referenced this issue Aug 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.