question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add indexes if they do not already exist

See original GitHub issue

Hi Tim,

I’ve been trying to add indexes to existing columns in postgres database.

I think I found a way to add them using (I found this documented in the knex source):

table.index('column_name')

The problem is that the indexes may already exist in which case I get the following error:

Error: model ensureSchema { [error: relation "models_column_name_index" already exists]
  name: 'error',
  length: 104,
  severity: 'ERROR',
  code: '42P07',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  file: 'index.c',
  line: '777',
  routine: 'index_create' }

I’ve been working at trying to do this for around 6 hours using the Knex.schema.table(table_name, (table) -> ).exec(callback) inner block and feel like I must be missing something. I originally tried to find a way to get access to a column builder, but I couldn’t use the table.type(column_name).index() syntax because the column already existed and even if it did, I assume that I would get the same index already existing error.

Is there a lower level way to check for indexes existing? Ideally, it would be general purpose enough that you could check if a column was indexed, primary, nullable, unique, etc.

Thank you in advance for any advice and pseudo code.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
yamikuronuecommented, Oct 18, 2016

I agree, this is highly unintuitive. The docs seem to imply that the callback is only being called if the table did not exist, so it can be created; otherwise, why would you need to call the table creation API anyway?

2reactions
bennycodecommented, Dec 13, 2017

I also expected that knex.schema.createTableIfNotExists doesn’t run the callback if the table already exists. So the official workaround is to check with hasTable beforehand?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add index to table if it does not exist - mysql - Stack Overflow
I want to add an index to a table by using the ALTER syntax, but first check if it already exists on the...
Read more >
If Exists Drop Index If Not Exists Create Index
I would say to create the index if it doesn't exist. Using the DROP EXISTING implies that the index already exists. We do...
Read more >
Only create an index when it doesn't exist - Ask TOM
Only create an index when it doesn't exist This is probably a simple question, though finding the answer has been surprisingly difficult: As ......
Read more >
MySQL: Create index If not exist? - Quora
select if ( · exists( · select distinct index_name from information_schema.statistics · where table_schema = 'schema_db_name' · and table_name = 'tab_name' and ...
Read more >
Don't add database index if it already exists in Rails
Rails 6 added an option if_not_exists to the create_table method which will not create the table if it already exists. This option is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found