I have this table in which the primary key is the combination of an unsigned big int as an ID and then a datetime variable that is set as YYYY-MM-DD HH-MM-SS
i got the idea from the comments in: MySQL: Using DATETIME as primary key
The problem I am having is when I try to insert a new record with the same ID but different datetime, I am getting an error saying violation of the primary key constraint of duplicate IDs.
My guess is that MySQL is only comparing the YYYY-MM-DD part of the datetime data type since it's doing a string comparison, but I'm not sure... it's just my theory.
I was hoping someone who has used datetime as part of their primary key would have some insight.
the php code is right because obviously the first record is being created but a second record with the same ID is not getting created.
I was trying not to display my SQL code but here it is
CREATE TABLE userPosts(
postID SERIAL NOT NULL,
username varchar(21) NOT NULL,
time datetime NOT NULL,
text varchar(500),
type varchar(1) NOT NULL CHECK(type='t' OR type='f' OR type='u' OR type='b' OR type='c'),
-- visits integer NOT NULL DEFAULT 0,
CONSTRAINT FK_users_userPosts FOREIGN KEY(username) REFERENCES users(username) ON DELETE CASCADE,
CONSTRAINT PK_userPosts_username_postID_time PRIMARY KEY(postID,time)
);
I'm using a hierarchy model where this table would be described as the ISA table.
Wow, I think it's because I assigned ID as a SERIAL data type which I believe to be UNIQUE... I'll have to re-implement my php to auto increment ID's instead of the database doing it.
I'll re-implement my code and keep you guys posted.