I have the following problem with a pretty simple MySQL database table creation:
SQL query:
CREATE TABLE IF NOT EXISTS comments(
commentId INT NOT NULL AUTO_INCREMENT,
comment VARCHAR(1024) NOT NULL,
commentdate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
profileId INT NOT NULL,
PRIMARY KEY(commentId),
CONSTRAINT fk_ProfilesProfileId FOREIGN KEY(profileId) REFERENCES profiles.profileId ON UPDATE RESTRICT ON DELETE CASCADE
)Engine=InnoDB DEFAULT CHARSET utf8 COLLATE utf8_general_ci
Error:
MySQL said: Documentation #1215 - Cannot add foreign key constraint
As you can see, the error number is 1215, which would indicate that the problem is about Foreign Key and Primary Key in comments and profiles tables (respectively) being different types. However, I have checked multiple times that they both are INT(11) and signed.
Here are queries for both of them:
'profiles' -table
CREATE TABLE IF NOT EXISTS profiles(
profileId INT NOT NULL AUTO_INCREMENT,
corpname VARCHAR(512) NOT NULL DEFAULT 'VIRHE',
corpserial VARCHAR(16) NOT NULL DEFAULT 'VIRHE',
loginusername VARCHAR(128) NOT NULL,
loginpassword VARCHAR(128) NOT NULL,
profilephone VARCHAR(16),
mapsphone VARCHAR(16),
added TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
done TIMESTAMP DEFAULT NULL,
coderequest TIMESTAMP DEFAULT NULL,
confirmed TIMESTAMP DEFAULT NULL,
PRIMARY KEY(profileId),
INDEX corpnameIndex (corpname),
INDEX corpserialIndex (corpserial)
)Engine=InnoDB DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
'comments' -table
CREATE TABLE IF NOT EXISTS comments(
commentId INT NOT NULL AUTO_INCREMENT,
comment VARCHAR(1024) NOT NULL,
commentdate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
profileId INT NOT NULL,
PRIMARY KEY(commentId),
CONSTRAINT fk_ProfilesProfileId FOREIGN KEY(profileId) REFERENCES profiles.profileId ON UPDATE RESTRICT ON DELETE CASCADE
)Engine=InnoDB DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
It is possible that I am dumb, and I'm just missing something. Hopefully it's so.
done TIMESTAMP DEFAULT NULL,and this works? no needDEFAULTkeyword, just setNULL. and also, why useTIMESTAMPtype andNULLtogether ?doneis not null, then the profile has been done. If it's done, I need to get the date when it was done. That's my reasoning forTIMESTAMPandNULL. I am aware thatTIMESTAMP DEFAULT NULLisn't required. I just like to have clean SQL-queries for clarification in documents that are important (in this case, my graduation work).