简体   繁体   中英

How to use HasMany Relationship in AdonisJs

I am trying to do hasMany relationship in my adonisjs project on Post and User model

I have a column of user_id in my database column and i have tried the below code

posts () {
    return this.hasMany('App/Models/Post')
  }

in my User model but anytime i make a post i get this ER_NO_DEFAULT_FOR_FIELD: Field 'user_id' doesn't have a default value

The user_id field in the post model cannot be null.

You can set a default value in the post migration

// every post has a default owner
table.integer('user_id').defaultTo(1)

or allow the relationship to be null

table.integer('user_id').nullable()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM