Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

storing JSON data in mysql

I have an organization table for example

ID, name, address, phone, email, etc...

Now I want to add multiple addresses, phones and emails.

Is it a good way to store json data in email column like this

[ "[email protected]", "[email protected]", "[email protected]" ]

Or create another table just for emails and another for phones etc...

If storing json data is better - what is the best way to use it in rails?

like image 982
Nick Sanders Avatar asked Apr 26 '12 08:04

Nick Sanders


2 Answers

Here is what I found

http://api.rubyonrails.org/classes/ActiveRecord/Base.html

Saving arrays, hashes, and other non-mappable objects in text columns

like image 152
Nick Sanders Avatar answered Oct 17 '22 02:10

Nick Sanders


Storing data as a JSON string in a single database field means that you will not be able to manipulate/query the data using SQL - which kind of defeats the point of storing the data in a database, you may as well store it in a text file.

I'd recommend a one-to-many relationship between your organization table and tables for email addresses and phone numbers. See video explaining different relationship types

like image 26
rgvcorley Avatar answered Oct 17 '22 02:10

rgvcorley