我一直致力于使用PostgreSQL数据库的NodeJS项目。 我正在尝试实现迁移到数据库。另外,使用Sequelize。设置迁移文件夹和配置后,它会在运行db:migrate
时抛出错误错误是: “从v4.0.0开始,需要明确提供方言”
答案 0 :(得分:8)
我的解决方案基于我为NODE_ENV
变量设置的内容。
echo $NODE_ENV
如果您没有为该变量设置任何内容,请尝试使用以下内容进行设置:
export NODE_ENV=development
如果 值存在,请确保您的配置文件中有 值的条目。对我来说,我喜欢使用local
。所以我不得不将我的配置更新为:
{
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'
}
}
答案 1 :(得分:1)
检查一次方言。
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' */
});
答案 2 :(得分:0)
您是否忘记将方言添加到配置中? 见:http://docs.sequelizejs.com/manual/tutorial/migrations.html
答案 3 :(得分:0)
检查配置文件(env名称)
{
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'
}
}

答案 4 :(得分:0)
如果您在运行npm server
您可能会收到该错误。因此每次重新启动应用程序以进行更改。 你将不得不再次出口
export DATABASE_URL=<your-db-url>
答案 5 :(得分:0)
我遇到了同样的错误,并且在代码中看到了这个错误。
title: {
type: Sequelize,
allowNull: false,
},
以此更改我的代码,问题得以解决:
title: {
type: Sequelize.STRING,
allowNull: false,
},
答案 6 :(得分:0)
我的问题是我没有正确指向配置文件。它说“已成功加载”,但我没有指向正确的文件。
它应该显示为“已加载的配置文件“ [某些路径] /config.js”。”
答案 7 :(得分:0)
如果模型架构中存在错误,也可能导致此错误。
我有:
middle_name: {
type: Sequelize.Sequelize,
allowNull: false,
}
应该是哪个:
middle_name: {
type: Sequelize.STRING,
allowNull: false,
}
答案 8 :(得分:0)
就我而言,我将config.js声明为:
module.exports = {
production: {
database: process.env.DB_PROD_DATABASE,
username: process.env.DB_PROD_USERNAME,
password: process.env.DB_PROD_PASSWORD,
options: {
host: process.env.DB_PROD_HOST,
port: process.env.DB_PROD_PORT,
dialect: 'postgres',
define: {
paranoid: true,
timestamp: true,
freezeTableName: true,
underscored: false
}
}
},
development: {
database: process.env.DB_DEV_DATABASE || 'database_name',
username: process.env.DB_DEV_USERNAME || 'user_name',
password: process.env.DB_DEV_PASSWORD || 'pass',
host: process.env.DB_DEV_HOST || 'localhost',
port: process.env.DB_DEV_PORT || 5432,
dialect: 'postgres',
define: {
paranoid: true,
timestamp: true,
freezeTableName: true,
underscored: false
}
}
}
但是应该像这样:
module.exports = {
production: {
database: process.env.DB_PROD_DATABASE,
username: process.env.DB_PROD_USERNAME,
password: process.env.DB_PROD_PASSWORD,
options: {
host: process.env.DB_PROD_HOST,
port: process.env.DB_PROD_PORT,
dialect: 'postgres',
define: {
paranoid: true,
timestamp: true,
freezeTableName: true,
underscored: false
}
}
},
development: {
database: 'database_name',
username: 'user_name',
password: 'pass',
host: 'localhost',
port: 5432,
dialect: 'postgres',
define: {
paranoid: true,
timestamp: true,
freezeTableName: true,
underscored: false
}
}
}
没有process.env.DB_DEV_DATABASE || 'database_name'
例如
答案 9 :(得分:0)
我认为您在项目中遗漏了 .env 文件
答案 10 :(得分:0)
如果您遇到此错误,则必须在 password_for_rootUser 之后以对象形式添加其他参数,并需要根据您的 RDBMS 指定这些属性
var Sequilize=require('sequelize');
var connection =new Sequilize('db_name','root_user_name','password_for_rootUser',{
host:'localhost',
dialect:'mysql'|'mariadb'|'sqlite'|'postgress'|'mssql',
pool:{
max:5,
min:0,
idle:10000
},
//for sqlite only
storage:path/to/database.sqlite
});
var Article=connection.define('tableName',{
title:Sequilize.STRING, // your cloumn name with data type
body:Sequilize.TEXT // your cloumn name with data type
});
connection.sync();
希望这能解决您的问题
答案 11 :(得分:0)
就我而言,问题是我从 config.ts 导出配置的方式
export const = {}
无效
但 module.exports
有效:
module.exports = {
development: {
dialect: process.env.DB_DIALECT,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME_DEVELOPMENT,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
},
test: {
dialect: process.env.DB_DIALECT,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME_DEVELOPMENT,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
},
production: {
dialect: process.env.DB_DIALECT,
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME_DEVELOPMENT,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
},
};