Nikito4ka_ka
@Nikito4ka_ka

Как решить ошибку Server error: Cannot add foreign key constraint?

Здравствуйте, сейчас только начал погружаться в изучение отношений в таблицах mysql.
проект использует react.js node,js sequalize и mysql

При старте возникает ошибка:
[0] Executing (default): CREATE TABLE IF NOT EXISTS `properties` (`id` BIGINT(20) UNSIGNED auto_increment , `name` VARCHAR(255) NOT NULL UNIQUE, `propertySpecificationId` BIGINT(20) UNSIGNED NOT NULL, `statusId` BIGINT(20) UNSIGNED, PRIMARY KEY (`id`), FOREIGN KEY (`propertySpecificationId`) REFERENCES `propertySpecification` (`id`), FOREIGN KEY (`statusId`) REFERENCES `status` (`id`)) ENGINE=InnoDB;
[0] Server error: Cannot add foreign key constraint


Пожалуйста подскажите как решить эту проблему

Модели:
property

const sequelize = require('../../routes/db-config');
const { DataTypes } = require('sequelize');
const {
	PropertySpecification,
} = require('./additionally/propertySpecification');
const { Status } = require('./additionally/status');

const Property = sequelize.define('property', {
	id: {
		type: DataTypes.BIGINT(20).UNSIGNED,
		primaryKey: true,
		autoIncrement: true,
	},
	name: {
		type: DataTypes.STRING(255),
		unique: true,
		allowNull: false,
	},
	propertySpecificationId: {
		type: DataTypes.BIGINT(20).UNSIGNED,
		allowNull: false,
		references: {
			model: 'propertySpecification',
			key: 'id',
		},
	},
	statusId: {
		type: DataTypes.BIGINT(20).UNSIGNED,
		allowNull: true,
		references: {
			model: 'status',
			key: 'id',
		},
	},
});
Property.hasOne(PropertySpecification, {
	foreignKey: 'propertySpecificationId',
	onDelete: 'CASCADE',
});
PropertySpecification.belongsTo(Property, {
	foreignKey: 'propertyId',
	onDelete: 'CASCADE',
});
Property.hasOne(Status, {
	foreignKey: 'statusId',
	onDelete: 'SET NULL',
});
Status.belongsTo(Property, {
	foreignKey: 'propertyId',
	onDelete: 'SET NULL',
});

module.exports = {
	Property,
};


propertySpecification

const sequelize = require('../../../routes/db-config');
const { DataTypes } = require('sequelize');
const { Property } = require('../property');
const { Category } = require('./category');
const { City } = require('./city');
const { TypeProperty } = require('./typeProperty');

const PropertySpecification = sequelize.define('propertySpecification', {
	id: {
		type: DataTypes.BIGINT(20).UNSIGNED,
		primaryKey: true,
		autoIncrement: true,
	},
	propertyId: {
		type: DataTypes.BIGINT(20).UNSIGNED,
		allowNull: false,
		references: {
			model: 'property',
			key: 'id',
		},
	},
	typePropertyId: {
		type: DataTypes.BIGINT(20).UNSIGNED,
		allowNull: true,
		references: {
			model: 'typeProperty',
			key: 'id',
		},
	},
	categoryId: {
		type: DataTypes.BIGINT(20).UNSIGNED,
		allowNull: true,
		references: {
			model: 'category',
			key: 'id',
		},
	},
	cityId: {
		type: DataTypes.BIGINT(20).UNSIGNED,
		allowNull: true,
		references: {
			model: 'city',
			key: 'id',
		},
	},
	bedrooms: {
		type: DataTypes.INTEGER,
		allowNull: false,
	},
	bathrooms: {
		type: DataTypes.INTEGER,
		allowNull: false,
	},
	area: {
		type: DataTypes.STRING(255),
		allowNull: false,
	},
	map: {
		type: DataTypes.STRING(255),
		allowNull: false,
	},
	dateAvailable: {
		type: DataTypes.STRING(255),
		allowNull: true,
	},
	size: {
		type: DataTypes.FLOAT(20).UNSIGNED,
		allowNull: false,
	},
	quality: {
		type: DataTypes.STRING(30),
		allowNull: false,
	},
	view: {
		type: DataTypes.STRING(30),
		allowNull: true,
	},
	parking: {
		type: DataTypes.STRING(30),
		allowNull: true,
	},
	conditioner: {
		type: DataTypes.STRING(30),
		allowNull: true,
	},
	balcony: {
		type: DataTypes.STRING(30),
		allowNull: true,
	},
	pool: {
		type: DataTypes.STRING(30),
		allowNull: true,
	},
	steamRoom: {
		type: DataTypes.STRING(30),
		allowNull: true,
	},
	gym: {
		type: DataTypes.STRING(30),
		allowNull: true,
	},
	security: {
		type: DataTypes.STRING(30),
		allowNull: true,
	},
	maintenance: {
		type: DataTypes.STRING(30),
		allowNull: true,
	},
	floor: {
		type: DataTypes.STRING(30),
		allowNull: true,
	},
	near: {
		type: DataTypes.TEXT('long'),
		allowNull: true,
	},
	Description: {
		type: DataTypes.TEXT('long'),
		allowNull: true,
	},
	imageUrl: {
		type: DataTypes.TEXT('long'),
		allowNull: false,
	},
});

PropertySpecification.hasOne(TypeProperty, {
	foreignKey: 'typePropertyId',
	onDelete: 'SET NULL',
});
TypeProperty.belongsTo(PropertySpecification, {
	foreignKey: 'propertySpecificationId',
	onDelete: 'SET NULL',
});
PropertySpecification.hasOne(Category, {
	foreignKey: 'categoryId',
	onDelete: 'SET NULL',
});
Category.belongsTo(PropertySpecification, {
	foreignKey: 'propertySpecificationId',
	onDelete: 'SET NULL',
});
PropertySpecification.hasOne(City, {
	foreignKey: 'cityId',
	onDelete: 'SET NULL',
});
City.belongsTo(PropertySpecification, {
	foreignKey: 'propertySpecificationId',
	onDelete: 'SET NULL',
});

module.exports = {
	PropertySpecification,
};


status

const sequelize = require('../../../routes/db-config');
const { DataTypes } = require('sequelize');
const { Property } = require('../property');

const Status = sequelize.define('status', {
	id: {
		type: DataTypes.BIGINT(20).UNSIGNED,
		primaryKey: true,
		autoIncrement: true,
	},
	nameStatus: {
		type: DataTypes.STRING(255),
		unique: true,
		allowNull: false,
	},
	propertyId: {
		type: DataTypes.BIGINT(20).UNSIGNED,
		allowNull: true,
		references: {
			model: 'property',
			key: 'id',
		},
	},
});

module.exports = {
	Status,
};

  • Вопрос задан
  • 60 просмотров
Пригласить эксперта
Ответы на вопрос 1
toxa82
@toxa82
FOREIGN KEY (`statusId`) REFERENCES `status` (`id`)
Не вижу описания этой модели, таблица status на которую вы ссылаетесь существует?
Выполните этот запрос вручную, увидите полное описание ошибки, думаю будет понятней в какую сторону смотреть.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы