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

Sequelize intellisense doesn't work in Visual Studio Code #11103

Closed
1 of 7 tasks
pavledev opened this issue Jun 23, 2019 · 41 comments
Closed
1 of 7 tasks

Sequelize intellisense doesn't work in Visual Studio Code #11103

pavledev opened this issue Jun 23, 2019 · 41 comments
Assignees
Labels
status: wip For issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started. type: docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference.

Comments

@pavledev
Copy link

pavledev commented Jun 23, 2019

What are you doing?

I installed sequelize package inside Visual Studio Code and I imported it. Then I typed command new Sequelize and I opened brace and intellisense didn't appear. I also tried to define model and after typing model name and dot intellisense again didn't appear.
How to fix this problem?

Environment

Dialect:

  • mysql
  • postgres
  • sqlite
  • mssql
  • any
    Dialect library version: mysql2 1.6.5
    Database version: XXX
    Sequelize version: 5.8.9
    Node Version: v11.12.0
    OS: Windows 10 x64
    If TypeScript related: TypeScript version: XXX
    Tested with latest release:
  • No
  • Yes, specify that version:
@kuskhan
Copy link

kuskhan commented Jun 25, 2019

I have the same problem here.

@raghavgrover21
Copy link

raghavgrover21 commented Jul 1, 2019

same. vs code version - 1.35.1(latest)

@Platiplus
Copy link

I'm having the same problem - VS Code v1.35.1

@Flippittiflop
Copy link

Me too.
image

@renecruzATX
Copy link

Same but now on Version: 1.36.1

@papb
Copy link
Member

papb commented Jul 11, 2019

This looks like a bug with VSCode to be honest... I just tried to reproduce, and the following happened:

  1. I installed Sequelize on an empty local folder and tried to reproduce this problem, and reproduced it successfully

  2. I navigated to node_modules/sequelize/types/index.d.ts and opened it as a new tab in VSCode and then closed it

  3. I went back to my JS file and intellisense magically started working.

Can anyone check if this "solves" the problem as well? @kuskhan @raghavgrover21 @Platiplus @Flippittiflop @renecruzATX

@raghavgrover21
Copy link

@papb no its not working, tried it with my 2 projects just to be sure.

@Platiplus
Copy link

Platiplus commented Jul 12, 2019

This looks like a bug with VSCode to be honest... I just tried to reproduce, and the following happened:

  1. I installed Sequelize on an empty local folder and tried to reproduce this problem, and reproduced it successfully
  2. I navigated to node_modules/sequelize/types/index.d.ts and opened it as a new tab in VSCode and then closed it
  3. I went back to my JS file and intellisense magically started working.

Can anyone check if this "solves" the problem as well? @kuskhan @raghavgrover21 @Platiplus @Flippittiflop @renecruzATX

That didn't solved the issue for me, the intellisense works on previous versions, but not in latest stable or 6.0.0.

What worked for me but it doesn't make sense is the following:

image

Using "const Sequelize = require('sequelize').Sequelize;" works.

But that only works in the connection file, when you try to import it for the modules, the autocomplete kind of works when you don't use this syntax. I'm really lost here hahaha

@papb
Copy link
Member

papb commented Jul 12, 2019

@raghavgrover21 @Platiplus thanks for the replies. I am lost too. After I did what I said above, it started working and I can't reproduce the problem anymore. It started working for me magically...

@pavledev
Copy link
Author

pavledev commented Jul 12, 2019

This looks like a bug with VSCode to be honest... I just tried to reproduce, and the following happened:

  1. I installed Sequelize on an empty local folder and tried to reproduce this problem, and reproduced it successfully
  2. I navigated to node_modules/sequelize/types/index.d.ts and opened it as a new tab in VSCode and then closed it
  3. I went back to my JS file and intellisense magically started working.

Can anyone check if this "solves" the problem as well? @kuskhan @raghavgrover21 @Platiplus @Flippittiflop @renecruzATX

That didn't solved the issue for me, the intellisense works on previous versions, but not in latest stable or 6.0.0.

What worked for me but it doesn't make sense is the following:

image

Using "const Sequelize = require('sequelize').Sequelize;" works.

But that only works in the connection file, when you try to import it for the modules, the autocomplete kind of works when you don't use this syntax. I'm really lost here hahaha

I tried this way and it worked. Thank you very much @Platiplus

@renecruzATX
Copy link

Beautiful! Thanks @Platiplus! It worked like a charm. I have a database JS file that I created and that is where I deployed this trick. Anywhere I import that database file the Intellisense works fine. The hive mind works again!

@sarifmiaa
Copy link

It works if you import const Sequelize = require("sequelize").Sequelize in the main connection file where you make connection to your database.

@timstrasser
Copy link

const Sequelize = require("sequelize").Sequelize gets intellisense partly working, but (for me) intellisense is still not working within functions such as create.

@papb papb added status: awaiting investigation type: other For issues and PRs. Something that does not fit in any of the other "type:" labels. labels Jul 28, 2019
@papb papb self-assigned this Jul 28, 2019
@papb
Copy link
Member

papb commented Jul 30, 2019

Similar to #8616

@IvanJasenov
Copy link

On VSCode Version 1.36.1, it's also buggy but this one works
const Sequelize = require('sequelize').Sequelize;

Thanks, @Platiplus

@papb
Copy link
Member

papb commented Aug 3, 2019

Observe that const { Sequelize } = require('sequelize'); is more readable (IMO).

@nnaynnay
Copy link

This should also work.
import { Sequelize } from 'sequelize';

@sulian
Copy link

sulian commented Aug 20, 2019

I confirm the problem in vscode 1.37.1 under Windows 10

@papb
Copy link
Member

papb commented Aug 20, 2019

@sulian Does

const { Sequelize } = require('sequelize');

instead of

const Sequelize = require('sequelize');

fix it for you?

@SriSasanka8074
Copy link

Thanks , it works if we use

const { Sequelize } = require('sequelize');
instead of

const Sequelize = require('sequelize');

@sulian
Copy link

sulian commented Aug 29, 2019

@sulian Does

const { Sequelize } = require('sequelize');

instead of

const Sequelize = require('sequelize');

fix it for you?

It works

@papb papb added existing workaround For issues. There is a known workaround for this issue. status: understood For issues. Applied when the issue is understood / reproducible. type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense. and removed status: awaiting investigation labels Aug 29, 2019
@phoenix-glr
Copy link

It also gets intellisense partly working, but (for me) intellisense is still not working within functions such as create() or ...

@papb
Copy link
Member

papb commented Aug 29, 2019

@GhostGLR Can you show a SSCCE/MCVE/reprex where const { Sequelize } = require('sequelize'); does not give correct intellisense for create?

@phoenix-glr
Copy link

phoenix-glr commented Aug 30, 2019

Sorry I Was Wrong! It "const { Sequelize } = require('sequelize');" Works.
Thanks For Your Help.

@papb papb added type: docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference. and removed existing workaround For issues. There is a known workaround for this issue. type: other For issues and PRs. Something that does not fit in any of the other "type:" labels. type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense. labels Aug 30, 2019
@papb
Copy link
Member

papb commented Aug 30, 2019

I have changed this to a docs issue. This just has to be better explained in our tutorials.

@papb papb added status: wip For issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started. and removed status: understood For issues. Applied when the issue is understood / reproducible. labels Sep 6, 2019
@husalem
Copy link

husalem commented Dec 9, 2019

@sulian Does

const { Sequelize } = require('sequelize');

instead of

const Sequelize = require('sequelize');

fix it for you?

Thanks a lot.
That solved it on VSCode 1.40.2

@papb
Copy link
Member

papb commented Jan 16, 2020

This was fixed in #11825, now our tutorials always use the correct form:

const { Sequelize } = require('sequelize');

@papb papb closed this as completed Jan 16, 2020
@sharjeel288
Copy link

This looks like a bug with VSCode to be honest... I just tried to reproduce, and the following happened:

  1. I installed Sequelize on an empty local folder and tried to reproduce this problem, and reproduced it successfully
  2. I navigated to node_modules/sequelize/types/index.d.ts and opened it as a new tab in VSCode and then closed it
  3. I went back to my JS file and intellisense magically started working.

Can anyone check if this "solves" the problem as well? @kuskhan @raghavgrover21 @Platiplus @Flippittiflop @renecruzATX

That didn't solved the issue for me, the intellisense works on previous versions, but not in latest stable or 6.0.0.

What worked for me but it doesn't make sense is the following:

image

Using "const Sequelize = require('sequelize').Sequelize;" works.

But that only works in the connection file, when you try to import it for the modules, the autocomplete kind of works when you don't use this syntax. I'm really lost here hahaha

Thanks Buddy it Works for me perfectly

@ibrod83
Copy link

ibrod83 commented Mar 14, 2020

For those who are having trouble using this "trick" in other modules, you can add a JSDoc comment. For instance, i have this function, in the same file that creates a connection:

/**
 * @return {Sequelize} db//This will make visual code know that "db" is of type Sequelize.Sequelize
 */
function getDb() {
    return db;
}

And then when i use it in a model, the intellisense works:

const sequelize = getDb();

const Category = sequelize.define('category', {    
    name: { type: DataTypes.STRING },   

});

Untitled

@Juanpam
Copy link

Juanpam commented Apr 23, 2020

@papb The getting started section from the V5 docs is still showing the import without destructuring. I think docs should be updated accordingly so intellisense is working right from the start :)

@itsmejay80
Copy link

@sulian this worked for me:
const { Sequelize } = require('sequelize');
Thanx

@CycoStallion
Copy link

CycoStallion commented Jul 28, 2020

This should also work.
import { Sequelize } from 'sequelize';

This did it for me as well! Thank you @nnaynnay

@CycoStallion
Copy link

This looks like a bug with VSCode to be honest... I just tried to reproduce, and the following happened:

  1. I installed Sequelize on an empty local folder and tried to reproduce this problem, and reproduced it successfully
  2. I navigated to node_modules/sequelize/types/index.d.ts and opened it as a new tab in VSCode and then closed it
  3. I went back to my JS file and intellisense magically started working.

Can anyone check if this "solves" the problem as well? @kuskhan @raghavgrover21 @Platiplus @Flippittiflop @renecruzATX

That didn't solved the issue for me, the intellisense works on previous versions, but not in latest stable or 6.0.0.

What worked for me but it doesn't make sense is the following:

image

Using "const Sequelize = require('sequelize').Sequelize;" works.

But that only works in the connection file, when you try to import it for the modules, the autocomplete kind of works when you don't use this syntax. I'm really lost here hahaha

Thanks @Platiplus, this worked for me too

@adamjedrzejec
Copy link

Looks like Sequelize is NOT an export default.

In recent versions of JS it would look like this:
import { Sequelize } from 'sequelize'

but not like this:
import Sequelize from 'sequelize'

@imhazard17
Copy link

This looks like a bug with VSCode to be honest... I just tried to reproduce, and the following happened:

  1. I installed Sequelize on an empty local folder and tried to reproduce this problem, and reproduced it successfully
  2. I navigated to node_modules/sequelize/types/index.d.ts and opened it as a new tab in VSCode and then closed it
  3. I went back to my JS file and intellisense magically started working.

Can anyone check if this "solves" the problem as well? @kuskhan @raghavgrover21 @Platiplus @Flippittiflop @renecruzATX

That didn't solved the issue for me, the intellisense works on previous versions, but not in latest stable or 6.0.0.

What worked for me but it doesn't make sense is the following:

image

Using "const Sequelize = require('sequelize').Sequelize;" works.

But that only works in the connection file, when you try to import it for the modules, the autocomplete kind of works when you don't use this syntax. I'm really lost here hahaha

Thanks bro it worked for me...

@daweimau
Copy link

daweimau commented Jan 1, 2021

const Sequelize = require("sequelize").Sequelize gets intellisense partly working, but (for me) intellisense is still not working within functions such as create.

I experienced similar behaviour when working on my project inside a mounted Docker volume. I haven't figured out the "right" way to resolve this, but when I installed the npm packages in my local directory (not just inside the Docker container) then VScode intellisense was suddenly able to trace through all the class functions.

There's probably a "right" way of curing this other than mine.

NB: I'm also pretty sure that the mechanism for model definition can also obstruct intellisense. For example, I think using Sequelize model hooks, or sequelize-CLI function-creates-model syntax, can obstruct intellisense

@ArunAtGitHubb
Copy link

@sulian Does

const { Sequelize } = require('sequelize');

instead of

const Sequelize = require('sequelize');

fix it for you?

Yeah it fix my issue! thanks 🤗

@milon27
Copy link

milon27 commented Aug 10, 2021

Follow this you will get Intellisense works properly.

  1. /models/index.js
'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

let con;
if (config.use_env_variable) {
  con = new Sequelize(process.env[config.use_env_variable], config);
} else {
  con = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    const model = require(path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

/**
 * @return {Sequelize.Sequelize} con
 */
function getCon() {
  return con;
}

db.getCon = getCon

module.exports = db;
  1. /models/User.js
const { DataTypes } = require('sequelize');
const db = require('./index')

const User = db.getCon().define("User", {
    name: {
        type: DataTypes.STRING,
        allowNull: false
    },
    password: {
        type: DataTypes.STRING,
        allowNull: false
    }
})

module.exports = User

@haibert
Copy link

haibert commented Aug 12, 2022

Follow this you will get Intellisense works properly.

  1. /models/index.js
'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

let con;
if (config.use_env_variable) {
  con = new Sequelize(process.env[config.use_env_variable], config);
} else {
  con = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    const model = require(path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

/**
 * @return {Sequelize.Sequelize} con
 */
function getCon() {
  return con;
}

db.getCon = getCon

module.exports = db;
  1. /models/User.js
const { DataTypes } = require('sequelize');
const db = require('./index')

const User = db.getCon().define("User", {
    name: {
        type: DataTypes.STRING,
        allowNull: false
    },
    password: {
        type: DataTypes.STRING,
        allowNull: false
    }
})

module.exports = User

What if you are using an existing DB and used auto-sequelize to generate your models. so your models look like this

const Sequelize = require('sequelize').Sequelize

class client extends Sequelize.Model {
    static init(sequelize, DataTypes) {
        return super.init(
            {
                clientID: {
                    autoIncrement: true,
                    type: DataTypes.INTEGER.UNSIGNED,
                    allowNull: false,
                    primaryKey: true,
                },
                FirstName: {
                    type: DataTypes.STRING(100),
                    allowNull: false,
                },
                LastName: {
                    type: DataTypes.STRING(100),
                    allowNull: false,
                },
                email: {
                    type: DataTypes.STRING(254),
                    allowNull: false,
                },
                CustomerName: {
                    type: DataTypes.STRING(100),
                    allowNull: false,
                },
                BusinessAddress: {
                    type: DataTypes.STRING(100),
                    allowNull: true,
                },
                BusinessCity: {
                    type: DataTypes.STRING(45),
                    allowNull: true,
                },
                BusinessState: {
                    type: DataTypes.STRING(45),
                    allowNull: true,
                },
                BusinessZip: {
                    type: DataTypes.DECIMAL(10, 0),
                    allowNull: true,
                },
                BusinessCountry: {
                    type: DataTypes.STRING(45),
                    allowNull: true,
                    defaultValue: 'USA',
                },
                BusinessPhone: {
                    type: DataTypes.STRING(15),
                    allowNull: false,
                },
                BusinessFax: {
                    type: DataTypes.STRING(15),
                    allowNull: true,
                },
                MobilePhone: {
                    type: DataTypes.STRING(15),
                    allowNull: true,
                },
                ContactName: {
                    type: DataTypes.STRING(50),
                    allowNull: true,
                },
                ContactTitle: {
                    type: DataTypes.STRING(45),
                    allowNull: true,
                },
                ContactPhone: {
                    type: DataTypes.STRING(15),
                    allowNull: true,
                },
                ContactEmail: {
                    type: DataTypes.STRING(100),
                    allowNull: true,
                },
                Comments: {
                    type: DataTypes.STRING(255),
                    allowNull: true,
                },
                Status: {
                    type: DataTypes.ENUM('Y', 'N'),
                    allowNull: true,
                    defaultValue: 'Y',
                },
                openingBalance: {
                    type: DataTypes.DECIMAL(10, 2),
                    allowNull: false,
                },
                openingBalanceAsOf: {
                    type: DataTypes.DATEONLY,
                    allowNull: false,
                },
                customerSince: {
                    type: DataTypes.DATEONLY,
                    allowNull: false,
                },
                companyTicketAttributres: {
                    type: DataTypes.TEXT,
                    allowNull: false,
                },
                nickName: {
                    type: DataTypes.STRING(255),
                    allowNull: true,
                },
                urgentNote: {
                    type: DataTypes.TEXT,
                    allowNull: false,
                },
                urgentNoteEnabled: {
                    type: DataTypes.STRING(100),
                    allowNull: false,
                },
                suspended: {
                    type: DataTypes.STRING(20),
                    allowNull: false,
                },
                suspendedDate: {
                    type: DataTypes.DATE,
                    allowNull: false,
                },
                lastUpdated: {
                    type: DataTypes.STRING(200),
                    allowNull: false,
                },
                CustomerType: {
                    type: DataTypes.STRING(200),
                    allowNull: false,
                },
                createdClientPortalID: {
                    type: DataTypes.INTEGER,
                    allowNull: false,
                },
            },
            {
                sequelize,
                tableName: 'client',
                timestamps: false,
                indexes: [
                    {
                        name: 'PRIMARY',
                        unique: true,
                        using: 'BTREE',
                        fields: [{ name: 'clientID' }],
                    },
                    {
                        name: 'ContactName',
                        using: 'BTREE',
                        fields: [{ name: 'ContactName' }],
                    },
                    {
                        name: 'CustomerName',
                        using: 'BTREE',
                        fields: [{ name: 'CustomerName' }],
                    },
                    {
                        name: 'email',
                        using: 'BTREE',
                        fields: [{ name: 'email' }],
                    },
                    {
                        name: 'urgentNote',
                        type: 'FULLTEXT',
                        fields: [{ name: 'urgentNote' }],
                    },
                ],
            }
        )
    }
}

module.exports = (sequelize, DataTypes) => {
    return client.init(sequelize, DataTypes)
}

@feyton
Copy link

feyton commented Aug 19, 2022

@sulian Does

const { Sequelize } = require('sequelize');

instead of

const Sequelize = require('sequelize');

fix it for you?

This worked for me. Using the default import is the issue

@sanjarcode
Copy link

The default import doesn't solve the major problem. It enabled autocomplete for the Sequelize.*, but autocomplete still doesn't work if the model was created using sequelize.define() where sequelize is the db instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: wip For issues and PRs. Applied when the PR is not ready yet / when work to close the issue has started. type: docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference.
Projects
None yet
Development

No branches or pull requests