后续投掷:"relation "users“不存在”
Sequelize throwing: "relation "users" does not exist"
我有一个 User 模型,它与我的 Item 模型有 hasMany 关联:
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
email: DataTypes.STRING
}, {
underscored: true,
});
User.associate = function(models) {
// associations can be defined here
User.hasMany(models.Item, {
onDelete: 'CASCADE'
})
};
return User;
};
我有一个属于用户的项目模型:
module.exports = (sequelize, DataTypes) => {
const Item = sequelize.define('Item', {
title: DataTypes.STRING,
link: DataTypes.STRING,
user_id: DataTypes.INTEGER
}, {
underscored: true,
});
Item.associate = function (models) {
// associations can be defined here
Item.belongsTo(models.User)
};
return Item;
};
当我执行User.findAll()
我收到错误 relation "users" does not exist
。
这是执行的原始 SQL:
SELECT
"User"."id", "User"."first_name", "User"."last_name", "User"."email",
"User"."created_at" AS "createdAt", "User"."updated_at" AS "updatedAt",
"Items"."id" AS "Items.id", "Items"."title" AS "Items.title",
"Items"."link" AS "Items.link", "Items"."user_id" AS "Items.user_id",
"Items"."created_at" AS "Items.createdAt", "Items"."updated_at" AS "Items.updatedAt",
"Items"."user_id" AS "Items.UserId"
FROM "users" AS "User"
LEFT OUTER JOIN "items" AS "Items" ON "User"."id" = "Items"."user_id";
Items 属性前面有 "Items",这是正确的,但是 Users 前面有 "User",这是不正确的。也不知道为什么它说 FROM "users" AS "User"
而不是 FROM "Users"
在我的配置中,我使用了 underscored: true
并且我使用的是 postgres 数据库。我的 postgres 数据库中有一个 "Users" table 和一个 "Items" table。
"pg": "^7.12.1",
"sequelize": "^5.18.4"
"sequelize-cli": "^5.5.1"
切换 config/config.js 来自:
require('dotenv').config()
module.exports = {
development: {
url: process.env.DEV_DATABASE_URL,
dialect: 'postgres'
}
}
至:
require('dotenv').config()
module.exports = {
development: {
url: process.env.DEV_DATABASE_URL,
database: 'database_development',
dialect: 'postgres'
}
}
Sequelize 需要配置中的数据库密钥!
我有一个 User 模型,它与我的 Item 模型有 hasMany 关联:
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
email: DataTypes.STRING
}, {
underscored: true,
});
User.associate = function(models) {
// associations can be defined here
User.hasMany(models.Item, {
onDelete: 'CASCADE'
})
};
return User;
};
我有一个属于用户的项目模型:
module.exports = (sequelize, DataTypes) => {
const Item = sequelize.define('Item', {
title: DataTypes.STRING,
link: DataTypes.STRING,
user_id: DataTypes.INTEGER
}, {
underscored: true,
});
Item.associate = function (models) {
// associations can be defined here
Item.belongsTo(models.User)
};
return Item;
};
当我执行User.findAll()
我收到错误 relation "users" does not exist
。
这是执行的原始 SQL:
SELECT
"User"."id", "User"."first_name", "User"."last_name", "User"."email",
"User"."created_at" AS "createdAt", "User"."updated_at" AS "updatedAt",
"Items"."id" AS "Items.id", "Items"."title" AS "Items.title",
"Items"."link" AS "Items.link", "Items"."user_id" AS "Items.user_id",
"Items"."created_at" AS "Items.createdAt", "Items"."updated_at" AS "Items.updatedAt",
"Items"."user_id" AS "Items.UserId"
FROM "users" AS "User"
LEFT OUTER JOIN "items" AS "Items" ON "User"."id" = "Items"."user_id";
Items 属性前面有 "Items",这是正确的,但是 Users 前面有 "User",这是不正确的。也不知道为什么它说 FROM "users" AS "User"
而不是 FROM "Users"
在我的配置中,我使用了 underscored: true
并且我使用的是 postgres 数据库。我的 postgres 数据库中有一个 "Users" table 和一个 "Items" table。
"pg": "^7.12.1",
"sequelize": "^5.18.4"
"sequelize-cli": "^5.5.1"
切换 config/config.js 来自:
require('dotenv').config()
module.exports = {
development: {
url: process.env.DEV_DATABASE_URL,
dialect: 'postgres'
}
}
至:
require('dotenv').config()
module.exports = {
development: {
url: process.env.DEV_DATABASE_URL,
database: 'database_development',
dialect: 'postgres'
}
}
Sequelize 需要配置中的数据库密钥!