Pagina 1 di 1

[MySQL] ERROR 1215 (HY000): Cannot add foreign key constrain

Inviato: domenica 31 maggio 2015, 22:03
da TommyB1992
Da terminale sto cercando di creare una tabella di woltlab:

Codice: Seleziona tutto

DROP TABLE IF EXISTS wbb1_post;

CREATE TABLE `wbb1_post` (
  `postID` int(10) NOT NULL AUTO_INCREMENT,
  `threadID` int(10) NOT NULL,
  `userID` int(10) DEFAULT NULL,
  `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `subject` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `message` mediumtext COLLATE utf8_unicode_ci NOT NULL,
  `time` int(10) NOT NULL DEFAULT '0',
  `isDeleted` tinyint(1) NOT NULL DEFAULT '0',
  `isDisabled` tinyint(1) NOT NULL DEFAULT '0',
  `isClosed` tinyint(1) NOT NULL DEFAULT '0',
  `editorID` int(10) DEFAULT NULL,
  `editor` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `lastEditTime` int(10) NOT NULL DEFAULT '0',
  `editCount` mediumint(7) NOT NULL DEFAULT '0',
  `editReason` text COLLATE utf8_unicode_ci,
  `attachments` smallint(5) NOT NULL DEFAULT '0',
  `pollID` int(10) DEFAULT NULL,
  `enableSmilies` tinyint(1) NOT NULL DEFAULT '1',
  `enableHtml` tinyint(1) NOT NULL DEFAULT '0',
  `enableBBCodes` tinyint(1) NOT NULL DEFAULT '1',
  `showSignature` tinyint(1) NOT NULL DEFAULT '1',
  `ipAddress` varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `cumulativeLikes` mediumint(7) NOT NULL DEFAULT '0',
  `deleteTime` int(10) NOT NULL DEFAULT '0',
  PRIMARY KEY (`postID`),
  KEY `threadID` (`threadID`,`userID`),
  KEY `threadID_2` (`threadID`,`isDeleted`,`isDisabled`,`time`),
  KEY `isDeleted` (`isDeleted`),
  KEY `isDisabled` (`isDisabled`),
  KEY `ipAddress` (`ipAddress`),
  KEY `time` (`time`),
  KEY `abbc0439063d333ac79065f1379ac5ef_fk` (`userID`),
  KEY `fe4ab714a15afd3495126fede5ad0b6c_fk` (`editorID`),
  KEY `33917154f7a46447d164b6fb12be8f36_fk` (`pollID`),
  CONSTRAINT `33917154f7a46447d164b6fb12be8f36_fk` FOREIGN KEY (`pollID`) REFERENCES `wcf1_poll` (`pollID`) ON DELETE SET NULL,
  CONSTRAINT `87e5b9c2dcf7bf276097178fc13ee88c_fk` FOREIGN KEY (`threadID`) REFERENCES `wbb1_thread` (`threadID`) ON DELETE CASCADE,
  CONSTRAINT `abbc0439063d333ac79065f1379ac5ef_fk` FOREIGN KEY (`userID`) REFERENCES `wcf1_user` (`userID`) ON DELETE SET NULL,
  CONSTRAINT `fe4ab714a15afd3495126fede5ad0b6c_fk` FOREIGN KEY (`editorID`) REFERENCES `wcf1_user` (`userID`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=732 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Ma restituisce questo errore: ERROR 1215 (HY000): Cannot add foreign key constraint.

Re: [MySQL] ERROR 1215 (HY000): Cannot add foreign key const

Inviato: domenica 31 maggio 2015, 22:32
da cortinico
Molto probabile che il problema sia con le tabelle wcf1_poll, wbb1_thread, wcf1_user e wcf1_user.
Esistono? Se sì ci riesci a dare la loro struttura.

Comunque il problema te lo da perchè te stai cercando di imporgli un vincolo (con le righe CONSTRAINT) e lui non riesce ad impostarlo (probabilmente perchè manca una tabella/colonna).

Re: [MySQL] ERROR 1215 (HY000): Cannot add foreign key const

Inviato: domenica 31 maggio 2015, 22:43
da TommyB1992
Esistere esistono... che siano create ne dubito perchè mi ha dato un sacco di errori quando ho eseguito il comando per uppare il backup preso dal sito online.

Eccoti la struttura:

Codice: Seleziona tutto

DROP TABLE wcf1_poll_option_vote;

CREATE TABLE `wcf1_poll_option_vote` (
  `pollID` int(10) NOT NULL,
  `optionID` int(10) NOT NULL,
  `userID` int(10) NOT NULL,
  UNIQUE KEY `vote` (`pollID`,`optionID`,`userID`),
  KEY `optionID` (`optionID`,`userID`),
  KEY `c9e00e525afc4a380a811bce03765163_fk` (`userID`),
  CONSTRAINT `c9e00e525afc4a380a811bce03765163_fk` FOREIGN KEY (`userID`) REFERENCES `wcf1_user` (`userID`) ON DELETE CASCADE,
  CONSTRAINT `372dc2f0a816e523ffa584e7ca9bc6d1_fk` FOREIGN KEY (`optionID`) REFERENCES `wcf1_poll_option` (`optionID`) ON DELETE CASCADE,
  CONSTRAINT `71542a78895c0078bde85d6683473e72_fk` FOREIGN KEY (`pollID`) REFERENCES `wcf1_poll` (`pollID`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

DROP TABLE wbb1_thread;

CREATE TABLE `wbb1_thread` (
  `threadID` int(10) NOT NULL AUTO_INCREMENT,
  `boardID` int(10) NOT NULL,
  `languageID` int(10) DEFAULT NULL,
  `topic` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `firstPostID` int(10) DEFAULT NULL,
  `time` int(10) NOT NULL DEFAULT '0',
  `userID` int(10) DEFAULT NULL,
  `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `lastPostID` int(10) DEFAULT NULL,
  `lastPostTime` int(10) NOT NULL DEFAULT '0',
  `lastPosterID` int(10) DEFAULT NULL,
  `lastPoster` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `replies` mediumint(7) NOT NULL DEFAULT '0',
  `views` mediumint(7) NOT NULL DEFAULT '0',
  `attachments` smallint(5) NOT NULL DEFAULT '0',
  `polls` smallint(5) NOT NULL DEFAULT '0',
  `isAnnouncement` tinyint(1) NOT NULL DEFAULT '0',
  `isSticky` tinyint(1) NOT NULL DEFAULT '0',
  `isDisabled` tinyint(1) NOT NULL DEFAULT '0',
  `isClosed` tinyint(1) NOT NULL DEFAULT '0',
  `isDeleted` tinyint(1) NOT NULL DEFAULT '0',
  `movedThreadID` int(10) DEFAULT NULL,
  `movedTime` int(10) NOT NULL DEFAULT '0',
  `isDone` tinyint(1) NOT NULL DEFAULT '0',
  `cumulativeLikes` mediumint(7) NOT NULL DEFAULT '0',
  `hasLabels` tinyint(1) NOT NULL DEFAULT '0',
  `deleteTime` int(10) NOT NULL DEFAULT '0',
  PRIMARY KEY (`threadID`),
  KEY `lastPostTime` (`lastPostTime`),
  KEY `boardID` (`boardID`,`isAnnouncement`,`isSticky`,`lastPostTime`,`isDeleted`,`isDisabled`),
  KEY `boardID_2` (`boardID`,`isDeleted`,`isDisabled`,`movedThreadID`),
  KEY `isDeleted` (`isDeleted`),
  KEY `isDisabled` (`isDisabled`),
  KEY `movedTime` (`movedTime`),
  KEY `1cdda8daa964c0c0700631b849c02013_fk` (`languageID`),
  KEY `b211aa032918dd084baeea4e6e8ea93a_fk` (`userID`),
  KEY `0aad2779a020565e1308f065309570de_fk` (`lastPosterID`),
  KEY `2bb32f5b25b598e0ef78281b8bcf234f_fk` (`movedThreadID`),
  CONSTRAINT `2bb32f5b25b598e0ef78281b8bcf234f_fk` FOREIGN KEY (`movedThreadID`) REFERENCES `wbb1_thread` (`threadID`) ON DELETE CASCADE,
  CONSTRAINT `0aad2779a020565e1308f065309570de_fk` FOREIGN KEY (`lastPosterID`) REFERENCES `wcf1_user` (`userID`) ON DELETE SET NULL,
  CONSTRAINT `1cdda8daa964c0c0700631b849c02013_fk` FOREIGN KEY (`languageID`) REFERENCES `wcf1_language` (`languageID`) ON DELETE SET NULL,
  CONSTRAINT `b211aa032918dd084baeea4e6e8ea93a_fk` FOREIGN KEY (`userID`) REFERENCES `wcf1_user` (`userID`) ON DELETE SET NULL,
  CONSTRAINT `c2c6c02be5478db9ddfee44ea844f313_fk` FOREIGN KEY (`boardID`) REFERENCES `wbb1_board` (`boardID`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=607 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


DROP TABLE wcf1_user;

CREATE TABLE `wcf1_user` (
  `userID` int(10) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `password` varchar(100) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `accessToken` char(40) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `languageID` int(10) NOT NULL DEFAULT '0',
  `registrationDate` int(10) NOT NULL DEFAULT '0',
  `styleID` int(10) NOT NULL DEFAULT '0',
  `banned` tinyint(1) NOT NULL DEFAULT '0',
  `banReason` mediumtext COLLATE utf8_unicode_ci,
  `activationCode` int(10) NOT NULL DEFAULT '0',
  `lastLostPasswordRequestTime` int(10) NOT NULL DEFAULT '0',
  `lostPasswordKey` varchar(40) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `lastUsernameChange` int(10) NOT NULL DEFAULT '0',
  `newEmail` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `oldUsername` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `quitStarted` int(10) NOT NULL DEFAULT '0',
  `reactivationCode` int(10) NOT NULL DEFAULT '0',
  `registrationIpAddress` varchar(39) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `avatarID` int(10) DEFAULT NULL,
  `disableAvatar` tinyint(1) NOT NULL DEFAULT '0',
  `disableAvatarReason` text COLLATE utf8_unicode_ci,
  `enableGravatar` tinyint(1) NOT NULL DEFAULT '0',
  `signature` text COLLATE utf8_unicode_ci,
  `signatureEnableBBCodes` tinyint(1) NOT NULL DEFAULT '1',
  `signatureEnableHtml` tinyint(1) NOT NULL DEFAULT '0',
  `signatureEnableSmilies` tinyint(1) NOT NULL DEFAULT '1',
  `disableSignature` tinyint(1) NOT NULL DEFAULT '0',
  `disableSignatureReason` text COLLATE utf8_unicode_ci,
  `lastActivityTime` int(10) NOT NULL DEFAULT '0',
  `profileHits` int(10) NOT NULL DEFAULT '0',
  `rankID` int(10) DEFAULT NULL,
  `userTitle` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `userOnlineGroupID` int(10) DEFAULT NULL,
  `activityPoints` int(10) NOT NULL DEFAULT '0',
  `notificationMailToken` varchar(20) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `authData` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `likesReceived` mediumint(7) NOT NULL DEFAULT '0',
  `wbbPosts` int(10) NOT NULL DEFAULT '0',
  PRIMARY KEY (`userID`),
  KEY `username` (`username`),
  KEY `registrationDate` (`registrationDate`),
  KEY `styleID` (`styleID`),
  KEY `activationCode` (`activationCode`),
  KEY `registrationData` (`registrationIpAddress`,`registrationDate`),
  KEY `activityPoints` (`activityPoints`),
  KEY `likesReceived` (`likesReceived`),
  KEY `9b09180ae281646112d44b9aa2fc5048_fk` (`avatarID`),
  KEY `b9271ea6487420ab6ca3153da2379a0f_fk` (`rankID`),
  KEY `9fe2b1b2816319e14e168cdaa92175e7_fk` (`userOnlineGroupID`),
  KEY `wbbPosts` (`wbbPosts`),
  CONSTRAINT `9b09180ae281646112d44b9aa2fc5048_fk` FOREIGN KEY (`avatarID`) REFERENCES `wcf1_user_avatar` (`avatarID`) ON DELETE SET NULL,
  CONSTRAINT `9fe2b1b2816319e14e168cdaa92175e7_fk` FOREIGN KEY (`userOnlineGroupID`) REFERENCES `wcf1_user_group` (`groupID`) ON DELETE SET NULL,
  CONSTRAINT `b9271ea6487420ab6ca3153da2379a0f_fk` FOREIGN KEY (`rankID`) REFERENCES `wcf1_user_rank` (`rankID`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=55 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


Re: [MySQL] ERROR 1215 (HY000): Cannot add foreign key const

Inviato: domenica 31 maggio 2015, 22:57
da TommyB1992
Altra curiosità: succede qualcosa se elimino tutte le dipendenze togliendo le righe con CONSTRAINT? Perchè tanto io devo sviluppare e modificare dei moduli che poi andranno ad agire sul server online (solo che non posso toccargli niente li, come ovvio che sia).

Re: [MySQL] ERROR 1215 (HY000): Cannot add foreign key const

Inviato: domenica 31 maggio 2015, 23:03
da cortinico
Se togli tutti i costraint ti va (almeno io l'ho provato su un db vuoto Mysql e va...). L'unico problema e che non ha i i vincoli di integrità referenziale.
https://it.wikipedia.org/wiki/Vincolo_d ... ferenziale
Sarebbe bene che ci fossero, onde evitare di andare a finire in uno stato incosistente...

Re: [MySQL] ERROR 1215 (HY000): Cannot add foreign key const

Inviato: domenica 31 maggio 2015, 23:16
da TommyB1992
cortinico [url=http://forum.ubuntu-it.org/viewtopic.php?p=4764273#p4764273][img]http://forum.ubuntu-it.org/images/icons/icona-cita.gif[/img][/url] ha scritto:Se togli tutti i costraint ti va (almeno io l'ho provato su un db vuoto Mysql e va...). L'unico problema e che non ha i i vincoli di integrità referenziale.
https://it.wikipedia.org/wiki/Vincolo_d ... ferenziale
Sarebbe bene che ci fossero, onde evitare di andare a finire in uno stato incosistente...
ma tanto in locale per le cose che devo fare mi interessa relativamente che sia ottimale...
ti ringrazio.