rentease-backend/models/Rental.js

86 lines
1.7 KiB
JavaScript

const { DataTypes } = require('sequelize');
const sequelize = require('../config/db');
const Room = require('./Room');
const Tenant = require('./Tenant');
const Contract = require('./Contract');
const Rental = sequelize.define('Rental', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
roomId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: Room,
key: 'id'
}
},
tenantId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: Tenant,
key: 'id'
}
},
contractId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: Contract,
key: 'id'
}
},
startDate: {
type: DataTypes.DATE,
allowNull: false
},
endDate: {
type: DataTypes.DATE,
allowNull: false
},
rent: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
deposit: {
type: DataTypes.DECIMAL(10, 2),
allowNull: true
},
status: {
type: DataTypes.ENUM('active', 'expired'),
allowNull: false,
defaultValue: 'active'
},
remark: {
type: DataTypes.TEXT,
allowNull: true
},
createTime: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
},
updateTime: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
onUpdate: DataTypes.NOW
},
isDeleted: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0
}
}, {
tableName: 'rentals',
timestamps: false
});
// 建立关联
Rental.belongsTo(Room, { foreignKey: 'roomId' });
Rental.belongsTo(Tenant, { foreignKey: 'tenantId' });
Rental.belongsTo(Contract, { foreignKey: 'contractId' });
module.exports = Rental;