i working on voting table design using postgres 9.5 (but maybe question applicable sql in general). vote table should like:
------------------------- object | user | timestamp -------------------------
where object
, user
foreign keys ids corresponding own tables. have problem identifying should primary key.
i thought @ first make primary_key(object, user)
since use django server, doesn't support multicolumn primary key, not sure either performance since may access row using 1 of 2 columns (i.e. object
or user
), advantage idea works automatically unique key since same user shouldn't vote twice same object. , don't need additional indexes.
the other idea introduce auto
or serial
id field, don't think of advantage of using approach when table gets bigger. need introduce @ least unique_key(object, user)
adds computational complexity , data storage. not sure performance when select using 1 of 2 columns, may need 2 additional indexes object
, user
accelerate select operation since need heavily.
is there missing here? or there better idea?
django recognise "natural primary key" in case not supported. gut feeling right, django don't support it.
https://code.djangoproject.com/wiki/multiplecolumnprimarykeys
relational database designs use set of columns primary key table. when set includes more 1 column, known “composite” or “compound” primary key. (for more on terminology, here article discussing database keys).
currently django models support single column in set, denying many designs natural primary key of table multiple columns. django can't work these schemas; must instead introduce redundant single-column key (a “surrogate” key), forcing applications make arbitrary , otherwise-unnecessary choices key use table in given instance.
i'm less failure django personally. one option might form column primary key concatenating object , user.
remember there nothing special primary key. can add unique key
on pair of columns , make them both not null
.
you might find example useful.
Comments
Post a Comment