So I am little confused on how to store a list of phone numbers in one column. Here are the requirements:
I thought up of four possible solutions:
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.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.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
.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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With