问题
I have been working on a NodeJS project which uses PostgreSQL database. I am trying to implement migration to the database. Also, using Sequelize. After setting up the migration folder and config, it throws error while running db:migrate
The error is: "Dialect needs to be explicitly supplied as of v4.0.0"
回答1:
Solution for me was based on what I had set for my NODE_ENV variable.
echo $NODE_ENV
If you do not have anything set for that variable, try setting it with the following:
export NODE_ENV=development
If a value is present, make sure you have an entry in your config file for that value. For me, I like to use local. So I had to update my config to this:
{
local: {
username: 'root',
password: null,
database: 'database_dev',
host: '127.0.0.1',
dialect: 'postgres'
},
development: {
username: 'root',
password: null,
database: 'database_dev',
host: '127.0.0.1',
dialect: 'postgres'
},
test: {
username: 'root',
password: null,
database: 'database_test',
host: '127.0.0.1',
dialect: 'postgres'
},
production: {
username: 'root',
password: null,
database: 'database',
host: '127.0.0.1',
dialect: 'postgres'
}
}
回答2:
Check the dialect once.
const Sequelize = require('sequelize');
// Option 1: Passing parameters separately
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */
});
回答3:
I got same error and I saw this mistake in the code.
title: {
type: Sequelize,
allowNull: false,
},
Changed my code with this and problem is solved:
title: {
type: Sequelize.STRING,
allowNull: false,
},
回答4:
did you forget to add the dialect to your config? see: http://docs.sequelizejs.com/manual/tutorial/migrations.html
回答5:
Check your config file (env names)
{
development: {
username: 'root',
password: null,
database: 'database_development',
host: '127.0.0.1',
dialect: 'mysql'
},
test: {
username: 'root',
password: null,
database: 'database_test',
host: '127.0.0.1',
dialect: 'mysql'
},
production: {
username: 'root',
password: null,
database: 'database_production',
host: '127.0.0.1',
dialect: 'mysql'
}
}
回答6:
if you have not setup any .env variables before running your npm server
You are likely to get that error. so each time you restart app for changes. you will have to export again
export DATABASE_URL=<your-db-url>
回答7:
My issue was that I wasn't pointing to the config file properly. It said "successfully loaded" but I wasn't pointing to the right file.
It should say "Loaded configuration file "[some path]/config.js"."
来源:https://stackoverflow.com/questions/46694157/dialect-needs-to-be-explicitly-supplied-as-of-v4-0-0