Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default pivot table is not well referenced for ManyToMany relationship #698

Closed
tnga opened this issue Jun 19, 2021 · 3 comments
Closed

Default pivot table is not well referenced for ManyToMany relationship #698

tnga opened this issue Jun 19, 2021 · 3 comments
Assignees
Labels
Type: Invalid Doesn't really belong here. Maybe use discussion threads?

Comments

@tnga
Copy link

tnga commented Jun 19, 2021

Package version

14.2.0

Node.js and npm version

node 14.16.0 & npm 6.14.11

Given the following code with ManyToMany relationship between users table and roles table through user_role table,
Adonis may understand to reference the table user_role by default but it seems to reference a the table user_user_role instead. Maybe a double concatenation issue of the pivotForeign table (here the table users) ?

Sample Code (to reproduce the issue)

/// UserModel.ts
import {
  BaseModel,
  manyToMany,
  ManyToMany,
} from '@ioc:Adonis/Lucid/Orm'

import UserRole from 'App/Models/UserRole'
export default class User extends BaseModel {
  @manyToMany(() => UserRole)
  public roles: ManyToMany<typeof UserRole>
}
/// UserSeeder.ts
 import BaseSeeder from '@ioc:Adonis/Lucid/Seeder'
 import Role from 'App/Models/Role'
 import User from 'App/Models/User'

 export default class ManagerSeeder extends BaseSeeder {
  public async run() {
   const admin = new User()
   const admRole = await Role.findBy('tag', 'administrator')
   await admin.save()
   await admin.related('roles').attach([admRole.id]) // got error
   // error: insert into `user_user_role` (`user_id`, `user_role_id`) values (1, 3) 
   // - ER_NO_SUCH_TABLE: Table 'projectdb.user_user_role' doesn't exist
  }
 }

NB: The alternative solution is to use custom pivotTable. But the default behavior have to be sufficient.

@RomainLanz
Copy link
Member

Hey @tnga! 👋

Table's name are defined from the model name.

Since your model is named UserRole, the table will be named user_roles. Therefore, a ManyToMany with User will ended up being user_user_role.

@thetutlage
Copy link
Member

thetutlage commented Jun 21, 2021

Closing since not actionable. Please use discussion threads to clear your doubts

@thetutlage thetutlage added the Type: Invalid Doesn't really belong here. Maybe use discussion threads? label Jun 21, 2021
@tnga
Copy link
Author

tnga commented Jun 21, 2021

Yah! thanks @RomainLanz , well notice @thetutlage .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Invalid Doesn't really belong here. Maybe use discussion threads?
Projects
None yet
Development

No branches or pull requests

3 participants