Sequelize:表格没有正确关联

Sequelize: Tables aren't associating properly

我正在创建旅行计划应用程序。我想使用的模型是用户、旅行、活动和租赁。

这就是我打算将它们关联起来的方式:

一个用户可以有很多旅行、活动和租赁

很多次旅行可以有很多活动和租金

许多活动可以有很多租金

Table Associations

在我的节点服务器中,我创建了一个具有以下 Sequelize 关联的数据库文件:

User = sequelize.import('./models/user')
Trip = sequelize.import('./models/trip')
Activity = sequelize.import('./models/activity')
Rental = sequelize.import('./models/rental')

Activity.belongsTo(User)
User.hasMany(Activity, { as: 'Activities' })

Rental.belongsTo(User)
User.hasMany(Rental, { as: 'Rentals' })

Activity.belongsTo(Trip)
Trip.hasMany(Activity, { as: 'Activities' })

Rental.belongsTo(Trip)
Trip.hasMany(Rental, { as: 'Rentals' })

Rental.belongsTo(Activity)
Activity.hasMany(Rental, { as: 'Rentals' })

这是我的模型:

用户

module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('user', {
    firstName: {
        type: DataTypes.STRING,
        allowNull: false
    },
    lastName: {
        type: DataTypes.STRING,
        allowNull: false
    },
    email: {
        type: DataTypes.STRING,
        allowNull: false,
        unique: true
    },
    password: {
        type: DataTypes.STRING,
        allowNull: false
    },
    isAdmin: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false
    }
})

return User;

}

旅行

module.exports = (sequelize, DataTypes) => {
const Trip = sequelize.define('trip', {
    title: {
        type: DataTypes.STRING,
        allowNull: false
    },
    departLoc: {
        type: DataTypes.STRING,
        allowNull: false
    },
    arrivalLoc: {
        type: DataTypes.STRING,
        allowNull: false
    },
    startDate: {
        type: DataTypes.DATE,
        allowNull: false
    },
    endDate: {
        type: DataTypes.DATE,
        allowNull: false
    },
    travelMethod: {
        type: DataTypes.STRING,
        allowNull: true
    },
    reason: {
        type: DataTypes.STRING,
        allowNull: true
    },
    description: {
        type: DataTypes.STRING,
        allowNull: true
    }
})
return Trip;

}

activity

module.exports = (sequelize, DataTypes) => {
const Activity = sequelize.define('activity', {
    title: {
        type: DataTypes.STRING,
        allowNull: false
    },
    startDate: {
        type: DataTypes.DATE,
        allowNull: false
    },
    endDate: {
        type: DataTypes.DATE,
        allowNull: false
    },
    description: {
        type: DataTypes.STRING,
        allowNull: true
    }
})
return Activity;

}

租金

module.exports = (sequelize, DataTypes) => {
const Rental = sequelize.define('rental', {
    agency: {
        type: DataTypes.STRING,
        allowNull: false
    },
    item: {
        type: DataTypes.STRING,
        allowNull: false
    },
    startDate: {
        type: DataTypes.DATE,
        allowNull: false
    },
    endDate: {
        type: DataTypes.DATE,
        allowNull: false
    },
    description: {
        type: DataTypes.STRING,
        allowNull: true
    }
})
return Activity;

}

但是,当我检查我的 pgadmin 时,我看到

  1. 我的 activity table 有 userId、tripId 和一个不必要的 activityId 列
  2. 我的租金 table 没有任何关联。

每当我尝试向这些模型中插入新条目时,我都会收到如下错误:

PostMan Error Response

如何关联我的 table 以获得所需的关联?

Sequelize 根据 table 名称创建自己的列名称,因此当您定义 both-ways 来自源和目标的关联时 table 给出显式名称

Activity.belongsTo(User, {
   foreignKey: { name: 'UserId', allowNull: false }, // column name for userId in activity table
   targetKey: 'UserId',  // column name of id in User table
});
User.hasMany(Activity, {
   foreignKey: 'UserId', // name of column in Activity table
   sourceKey: 'UserId'   // name of column in User table
})

像这样写下其余的关联并尝试。

问题出在我的租赁模型上:它返回 Activity。

现在所有表都正确关联了。