fortune-postgres icon indicating copy to clipboard operation
fortune-postgres copied to clipboard

Query to information_schema.columns selects columns from all schemas

Open rwest202 opened this issue 5 years ago • 3 comments

https://github.com/fortunejs/fortune-postgres/blob/7d158ec79334584049fc2f8de404527eb24ae598/lib/index.js#L141-L142

Let's say I have a schema named schema-v1 and another named schema-v2 that both have a table called users. If schema-v1 has a column in users like users.email then the below if statement will wrongly think that the column exists in schema-v2. This is because information_schema.columns returns a list of columns in all schemas.

https://github.com/fortunejs/fortune-postgres/blob/7d158ec79334584049fc2f8de404527eb24ae598/lib/index.js#L188

Solution should be to specify the exact schema to search in.

rwest202 avatar Mar 14 '19 05:03 rwest202

Hmm I see, but does this cause any errors?

gr0uch avatar Mar 14 '19 06:03 gr0uch

It doesn't add the columns to the new schema, since it thinks they already exist. I was able to resolve my issue by removing the schema-v1. But it wasn't immediately obvious what was causing the issue.

rwest202 avatar Mar 14 '19 06:03 rwest202

Looks like there is a table_schema key that you can check against, so something like

if (!tableColumns[type].some(row => row.column_name === field && row.table_schema === currentSchema)

rwest202 avatar Mar 14 '19 06:03 rwest202