You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
And now, we need to build the relation with a SQL VALUE (1,2,1)
Here is the insert statement with Adonis Database rawQuery command
I write it in the User model @beforeUpdate()
@beforeUpdate()publicstaticasyncupdateClosureNode(model: Closure){letparentKey=model.getParentKey()if(parentKey){constparent=awaitmodel.convert2Model(model.getParentKey())parentKey=parent.getKey()}else{parentKey=0}constsql=` INSERT INTO user_closures (ancestor, descendant, distance) SELECT supertbl.ancestor, subtbl.descendant, supertbl.distance+subtbl.distance+1 FROM (SELECT * FROM user_closures WHERE descendant = 1) as supertbl JOIN user_closures as subtbl ON subtbl.ancestor = 2`awaitDatabase.connection(model.getConnectionName()).rawQuery(sql)}
And Above code can successfully insert the relation without calling convert2Model await this.constructor['findOrFail'](parameter)
It seems can not call a new model query inside the @beforeUpdate()
Any ideas?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Package version
"@adonisjs/core": "^5.8.6",
"@adonisjs/lucid": "^18.1.1",
"mysql2": "^2.3.3",
Node.js and npm version
mariadb:10.5.8-focal
node:v16.14.0
npm:8.13.0
Sample Code (to reproduce the issue)
The scenario is user A has a child user B,
I want to use
closure_table
to manage the user relationship.Here is the table SQL statement and the initialized value
And now, we need to build the relation with a
SQL VALUE (1,2,1)
Here is the insert statement with Adonis Database rawQuery command
I write it in the User model
@beforeUpdate()
And Above code can successfully insert the relation without calling convert2Model
await this.constructor['findOrFail'](parameter)
It seems can not call a new model query inside the
@beforeUpdate()
Any ideas?
Beta Was this translation helpful? Give feedback.
All reactions