reset password
Author Message
Vanquish39
Posts: 134
Posted 21:22 May 12, 2011 |

I have a question for yesterday's lab. 


THIS IS THE ANSWER CLASS

public class Answer {

    Integer     id;

    String      content;
    User        author;
    Date        timestamp;

    Set<Vote>   votes;

}

THIS IS THE ANSWERS TABLE

create table answers (
    id          integer primary key,
    content     text,
    author_id   integer,
    timestamp   timestamp,
    question_id integer references questions(id)
);

QUESTION IS:  Why isn't there a reference to the author_id?  Isn't it class type?  
              Shouldn't it be, author_id integer references users(id)?  

Thank you.

cysun
Posts: 2935
Posted 21:51 May 12, 2011 |

Yes, it should be

author_id integer references users(id)

I simply forgot to add the foreign key constraint. It doesn't really affect the design but the constraint should be there. Sorry about that.

Vanquish39
Posts: 134
Posted 22:00 May 12, 2011 |

Thank you Professor.  Also one more question

THIS IS THE VOTE CLASS

public class Vote {

    Answer      answer;
    User        voter;
    int         value;

}

THIS IS THE VOTES TABLE

create table votes (
    answer_id   integer references answers(id),
    voter_id    integer references users(id),
    value       integer,
  primary key(answer_id, voter_id)
);
 
QUESTION IS: SHOULDN'T THERE BE ANOTHER attribute called answer_id references answers(id)?  
In the table, the answer_id references answers(id) because it was a list created in a new table.  The voter_id 
references the user, and the value is an integer.  But what about the Answer answer; in the VOTE CLASS? 
It's missing from the table?  Also, how do you know that both answer_id, and voter_id should be primary keys?

Sorry if I'm asking too many questions.  




cysun
Posts: 2935
Posted 23:10 May 12, 2011 |

1. OO design and relational design do not have one-to-one correspondence. In the case with Answer and Vote, the following three class designs correspond to the same relational design:

a)

class Answer { Set<Vote> votes; ... }

class Vote  { Answer answer; ... }

b)

class Answer { Set<Vote> votes; ... }

class Vote { ... }

c)

class Answer { ... }

class Vote { Answer answer; ...}

2. Each user can only give one vote to an answer, so the combination of answer_id and vote_id must be unique (and not null).

Last edited by cysun at 23:11 May 12, 2011.