const { DataTypes } = require('sequelize'); const sequelize = require('../config/db'); const Region = require('./Region'); const Apartment = sequelize.define('Apartment', { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, regionId: { type: DataTypes.INTEGER, allowNull: false, references: { model: Region, key: 'id' } }, name: { type: DataTypes.STRING(50), allowNull: false }, address: { type: DataTypes.STRING(255), 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: 'apartments', timestamps: false }); // 建立关联 Apartment.belongsTo(Region, { foreignKey: 'regionId' }); Region.hasMany(Apartment, { foreignKey: 'regionId' }); module.exports = Apartment;