rentease-backend/models/Room.js

49 lines
1.0 KiB
JavaScript

const { DataTypes } = require('sequelize');
const sequelize = require('../config/db');
const Apartment = require('./Apartment');
const Room = sequelize.define('Room', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
apartmentId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: Apartment,
key: 'id'
}
},
roomNumber: {
type: DataTypes.STRING(20),
allowNull: false
},
area: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
price: {
type: DataTypes.DECIMAL(10, 2),
allowNull: false
},
status: {
type: DataTypes.ENUM('empty', 'rented', 'soon_expire', 'expired', 'cleaning', 'maintenance'),
allowNull: false,
defaultValue: 'empty'
},
createTime: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
}, {
tableName: 'rooms',
timestamps: false
});
// 建立关联
Room.belongsTo(Apartment, { foreignKey: 'apartmentId' });
Apartment.hasMany(Room, { foreignKey: 'apartmentId' });
module.exports = Room;