1

There is a website driven by Django where the user logs in and can answer thousands of questions. I want to persistently save whether a given user has answered a given question or not.

My approach so far is for the User table to have thousands of columns in the PostgreSQL database - a column for each question containing 0 if the question hasn't been answered yet and 1 if it has. However, there is a 1,600 column limit in PostgreSQL, which I haven't reached yet, but would reach eventually, so this approach won't do.

What is a viable alternative architecture to persistently save thousands of variables for each user?

8

Using a column per question makes it difficult to add more questions later.

Instead, model the relation between users and questions in your table. I.e. the questions are encoded explicitly as data, not implicitly in the structure of your table.

The table would have user_id and question_id columns, likely both with foreign key constraints to another table. These two columns are a composite key for the answers table. The table may have additional columns to store the answers. If you are only tracking whether the question has been answered, checking for existence of a particular (user, question) record might be sufficient, no further columns needed.

5

You should normalise your database schema. This means that instead of just having a User table, you would also have a Questions table containing information about each question, then you would also have an Answers link table where each row represents a particular user answering a particular question.

1

My approach so far is for the User table to have thousands of columns in the PostgreSQL database - a column for each question

Bad data model. You cannot write any sort of meaningful query against it.

Relational databases love to do things with rows; they're really not very good at mucking about with columns.

Normalise your data, creating a new table with one row per user & answered question combination - if no such row exists, they've not answered that question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.