Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails/postgres: What is the best way to store list of phone numbers for one user

So I am little confused on how to store a list of phone numbers in one column. Here are the requirements:

  1. Users can have more than one phone number.
  2. Phone numbers have to be unique, so if user A added a phone number that is used by user B then a validation error should be displayed.
  3. A default phone number should be selected if the user have more than one phone number.
  4. Solution have to be compatible with Postgresql.

I thought up of four possible solutions:

  1. HStore: making a phone_number field that stores a hash of all the phone numbers. e.g. {1=>"+1-800-123-1234", 2=>"9237492734", "default"=>1}. In this case I need to make lot of queries to make sure that a new phone number is unique, for example I need to query User.where("phone_number @> ('1' => '+1-800-123-1234')") then check in 2 User.where("phone_number @> ('2' => '+1-800-123-1234')") ... etc.
  2. Array of phone numbers in one field: phone_number will store comma separated phone numbers like "+1-800-123-1234,9237492734". Checking a existing phone number would be easy User.where("phone_number LIKE '%+1-800-123-1234%'") but will take a lot of time for the database to pick it up. default_phone will be added to the table as well or making the first phone number as the default one by convention.
  3. Limiting phone numbers to 3 (which is enough) and creating phone_number_1, phone_number_2 and phone_number_3 fields. Checking the uniqueness of a phone number will consume 3 queries. Also will require adding default_phone.
  4. Adding a new table phone_numbers (id:integer, user_id:integer, phone_number:string, default:boolean) and setting has_many relationship with User model. Which is not really seducing ... create a whole table for 1 field. But it have fast lookups and will have limitless phone numbers for each user.

Any ideas, hints and suggestions are greatly appreciated.

like image 819
wael34218 Avatar asked Nov 11 '12 10:11

wael34218


People also ask

Should phone numbers be stored as strings?

A phone number should always be stored as a string or text and never an integer. Some phone numbers generally use hyphens and possibly parentheses. Also, you might need to indicate the country code before the phone number such as +46 5555-555555.

Why is it a good idea to use a string variable to store a phone number than using an integer?

Mobile phone numbers are not stored as integers, as the integer data type holds values that have the potential to be used in calculations. There is no context for using a mobile phone number as part of a calculation, so it is stored as a STRING value.

Why you should generally store telephone numbers as a string not as a integer?

Telephone numbers are strings of digit characters, they are not integers. Consider for example: Expressing a telephone number in a different base would render it meaningless. Adding or multiplying two telephone numbers together, or any math operation on a phone number, is meaningless.

How are international phone numbers stored in a database?

E. 164 is an international format which defines a general format for international telephone numbers. Based on this formating, the number contains a country code (CC), a national destination code (NDC), and a subscriber number (SN). There can be up to 15 digits in an E.


1 Answers

Two tables is the solution to go after. You can potentially have multiple users who can be reached at the same phone number, such as a work number or home number that are landlines.

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  name TEXT NOT NULL,
  -- other bits of information
);

CREATE TABLE phone_numbers (
  user_id INTEGER REFERENCES users (id),
  phone_number TEXT NOT NULL,
  location TEXT NOT NULL, -- Mobile, home, work
  PRIMARY KEY (user_id, phone_number),
  INDEX (phone_number)
);

If you really want to enforce the 'each person has a unique phone number and that phone number cannot be used to contact anyone else', just add a UNIQUE constraint to the phone_number column.

like image 110
titanofold Avatar answered Sep 19 '22 15:09

titanofold