Creating tables outside migrations


#1

Hi, Adonis newbie has a problem. So can I create new database table in/via Controller and how it’s done? My app needs to create dynamic tables and I didn’t find any other way to create those than migrations. Thanks!


#2
  1. You could use migrations programmatically. Remember, it’s just JS code.

  2. You can always run a raw query, and just write the CREATE TABLE statement.

Although, tbh this has a bad code smell. Why do you need to create tables on the fly?


#3

Thx @moltar

I’m planning to re-write older PHP app with Adonis and that app creates dynamically new containers which can include multiple items. Those containers are dynamically created database tables. There is also one base table which keeps index of these containers.

Do you have suggestions how to accomplish this better way? I could write container contents to JSON object to base table, but contents management feels more complicated then.


#4

I am still not understanding the use case. But you can certainly use JSON for storage. Take a look at jsonb type in PostgreSQL. It allows you native JSON storage and querying.

Also, you can use automatic serialization of JSON with any database via Lucid.