Ready to get started?

Join Adocasts Plus for $8.00/mo, or sign into your account to get access to all of our lessons.

robot mascot smiling

Defining Our Lucid Models & Relationships

In this lesson, we'll convert our migrations into Lucid Models and define both sides of the relationships so they're ready to go.

Published
Sep 20, 24
Duration
19m 8s

Developer, dog lover, and burrito eater. Currently teaching AdonisJS, a fully featured NodeJS framework, and running Adocasts where I post new lessons weekly. Professionally, I work with JavaScript, .Net C#, and SQL Server.

Adocasts

Burlington, KY

Chapters

00:00 - User Model Relationships
03:15 - Role Model
03:52 - Organization Model
06:22 - AccessLevel, Difficulty, & Status Models
08:18 - Course Model
12:20 - Module Model
13:35 - Lesson Model
14:53 - EmailHistory Model
15:30 - OrganizationInvite Model
18:15 - PasswordResetToken Model

Ready to get started?

Join Adocasts Plus for $8.00/mo, or sign into your account to get access to all of our lessons.

Join The Discussion! (2 Comments)

Please sign in or sign up for free to join in on the dicussion.

  1. Commented 9 days ago

    If we want to define single or multi column indexes for our tables, do we do that in the migrations files?

    1

    Please sign in or sign up for free to reply

    1. Commented 9 days ago

      Yeah, you can do that via a migration! There is an index and dropIndex method for that. The KnexJS documentation has more info on the options these accept, which will vary depending on your database driver.

      import { BaseSchema } from '@adonisjs/lucid/schema'
      
      export default class extends BaseSchema {
        protected tableName = 'index_examples'
      
        async up() {
          this.schema.alterTable(this.tableName, (table) => {
            // single
            table.index('column_one')
      
            // multi-column
            table.index(['column_two', 'column_three'])
      
            // custom named
            table.index(['column_four'], 'custom_index_name', { /* additional opts */ })
          })
        }
      
        async down() {
          this.schema.alterTable(this.tableName, (table) => {
            // single
            table.dropIndex('column_one')
      
            // multi-column
            table.dropIndex(['column_two', 'column_three'])
      
            // custom named
            table.dropIndex(['column_four'], 'custom_index_name')
          })
        }
      }
      Copied!
      0

      Please sign in or sign up for free to reply