X Tutup
The Wayback Machine - https://web.archive.org/web/20201204202323/https://github.com/sequelize/sequelize/issues/5042
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

Option to auto create foreign key indexes #5042

Open
BridgeAR opened this issue Dec 14, 2015 · 8 comments
Open

Option to auto create foreign key indexes #5042

BridgeAR opened this issue Dec 14, 2015 · 8 comments

Comments

@BridgeAR
Copy link
Contributor

@BridgeAR BridgeAR commented Dec 14, 2015

If you create a foreign key constraint no further index will be auto created by postgres (I'm not sure what other DBs will do, I did not check), since there are a couple of different ways to create a index in that case.

But I'd say the average use case would be improved if sequelize would just add a normal btree index on the foreign key if you pass a option to the global sequelize instance. That way new tables would always benefit from that and you do not always have to think about it.

The performance difference is significant in that case.

So I'd say it would be a nice feature to add.

@mickhansen
Copy link
Contributor

@mickhansen mickhansen commented Dec 14, 2015

For 4.0 i'd like to have automatic indexes on all associations.
In 3.0 we could obviously have that behind a flag, and move that flag to true by default when we push the next major.

@BridgeAR
Copy link
Contributor Author

@BridgeAR BridgeAR commented Dec 14, 2015

👍

@alekbarszczewski
Copy link
Contributor

@alekbarszczewski alekbarszczewski commented Dec 22, 2015

This can be easily achieved by simple hook/plugin (code in coffeescript):

Sequelize = require 'sequelize'

_ = Sequelize.Utils._

sequelize = new Sequelize('postgres://alek:abcdef@localhost:5434/transaction_test')

sequelize.beforeSync (options) ->
  # check each foreign key
  for own attrName, attr of @attributes when attr.references
    # check if index is not already defined for this FK
    indexAlreadyDefined = _.find @options.indexes, (index) ->
      index.fields.length is 1 and attrName in index.fields
    continue if indexAlreadyDefined
    # add index on FK
    @options.indexes.push({
      fields: [attrName]
    })

User = sequelize.define('User')
Tag = sequelize.define('Tag')
Post = sequelize.define('Post')
PostTag = sequelize.define('PostTag')

Post.belongsTo(User)
Post.belongsToMany(Tag, { through: PostTag })

sequelize.sync({ force: true, logging: console.log })

# ...
# CREATE INDEX "posts__user_id" ON "Posts" ("UserId")
# ...
# CREATE INDEX "post_tags__post_id" ON "PostTags" ("PostId")
# CREATE INDEX "post_tags__tag_id" ON "PostTags" ("TagId")
@mickhansen
Copy link
Contributor

@mickhansen mickhansen commented Dec 22, 2015

@alekbarszczewski Neat! I think we might still want this in core though.

@stale stale bot added the stale label Jun 29, 2017
@stale stale bot closed this Jul 6, 2017
@ajmueller
Copy link

@ajmueller ajmueller commented Sep 19, 2018

@mickhansen did this make it into core? I don't see any references to indexing on the Associations page of the v4 documentation and references to indexes on the Model Definition page are for indexing other columns with no references to foreign keys.

@sushantdhiman sushantdhiman removed the stale label Sep 20, 2018
@sushantdhiman sushantdhiman reopened this Sep 20, 2018
@stale stale bot added the stale label Jul 23, 2019
@papb papb removed the stale label Jul 24, 2019
@ajmueller
Copy link

@ajmueller ajmueller commented Jul 25, 2019

Since @papb removed the stale label perhaps there's no need to comment as per the stale bot's recommendation, but in any case I do believe this is still an open issue.

@sequelize sequelize deleted a comment from stale bot Jul 25, 2019
@papb
Copy link
Member

@papb papb commented Jul 25, 2019

@ajmueller Yeah, it was marked as stale by mistake.

@cesumilo
Copy link

@cesumilo cesumilo commented Dec 24, 2019

Hi everyone, any news about this issue?

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

Successfully merging a pull request may close this issue.

None yet
7 participants
You can’t perform that action at this time.
X Tutup