How to write mysql views and triggers with adonis?

my db server is mysql

i can’t see in the doc how to write with adonis:

  1. mysql views
  2. mysql triggers

advises/hints ?

I don’t think they are AdonisJs issues, they are database concerns. Can you give a concrete example of what you are trying to achieve?

Hi @student

You can write them into migrations and use raw database queries in there
Inside migration file

/** @type {import('@adonisjs/lucid/src/Database')} */
const Database = use('Database')
up () {
  Database.raw('do things')

1 Like

Yeah @McSneaky has said, use the migration files to setup your views.


note that you need to use async/await when you do that. like

  async up () {
    await Database.raw(`CREATE VIEW v_routes AS 
                    SELECT, as origin_name, as destination_name, 
                         routes.from_location_id, routes.to_location_id
                    FROM routes
                    JOIN locations as origin 
                      ON routes.from_location_id =
                    JOIN locations as dest 
                      ON routes.to_location_id =;`)

  async down () {
    await Database.raw('DROP VIEW IF EXISTS v_routes;')