Question

I am building a website, where in different companies can create sub-sites, using either Rails or Drupal. When each user creates an account, he or she gets entirely independent instant of the application.

I however, have 2 choice when it comes to db. I can put all in a single db and use a prefix for naming the tables. For example the user table for client1 would be client1_user for next would be client2_user and so.

But I have no clue, based on what I should be deciding this. Is it dependent on language? Or technology? MyDb would be probably MariaDb or MySql.

No correct solution

OTHER TIPS

As suggested in the comments, the concepts you need are called multi-tenancy. This usually is not done with creating new tables for each new tenant, you rather have a table that holds all tenants information and then refer to tenant IDs in the other tables that hold the objects attached to a tenant.

Let met give you an example to clarify:

Say you want to built an online-shop application. You would create a table with all merchants in it. Every merchant could be identified with a separate subdomain. There would be other tables such as orders and products. Instead of creating new order/product tables for each merchant, these tables store the orders and products for all merchants, but reference the merchant they belong to via a merchant ID. Using Rails association this can easily be done in code.

In the merchant class you would define a relationship to the products like has_many :products, dependent: :destroy

And in the product class you would add belongs_to :merchant

Rails would than check if the product table has a merchant_id column and you're good to go.

On the controller site you have to make sure that every merchant can only see his own products, but loading the products like @merchant.products instead of just displaying all of them.

This is how multi-tenancy is usually realised in an Rails application. Although the data is combined on the database level in the same tables, the model and controller layers of Rails take care of separating the data and allowing access only to data attached to the logged in accounts. It is not necessary to create new tables for each tenant, which would cause massive overhead.

What kind of database you use, be it Postgres, MySQL is not really important since doing things like that is the heart of SQL anyway.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top