Giter VIP home page Giter VIP logo

Comments (4)

PhilWaldmann avatar PhilWaldmann commented on June 12, 2024

Hi @nibarulabs

You'll need to define the target relation. e.g.: this.hasMany('roles', {model: 'Role', through: 'roles_users', relation: 'roles'});.
There is currently no default to use the relation name as the target relation name.

btw:

const role = await Role.find(1);
const user = await User.create({name: 'John'});
user.roles.add(role);
await user.save() // <--- don't forget!

from openrecord.

nibarulabs avatar nibarulabs commented on June 12, 2024

Hi @PhilWaldmann, thanks for your reply.

Hmm, still getting the same error. I actually played with adding relation last night and was seeing the same behavior.

I've noticed something else too that might be fine, but wanted to double check. I added some console log to print out the role after I look it up and I noticed it adds the has many attributes to the class even though I'm using the through.

role: Role {
  relations: {},
  attributes: {
    id: 1,
    name: 'member',
    created_at: 2020-08-05T22:08:52.630Z,
    updated_at: 2020-08-05T22:08:52.629Z,
    creator_id: 1,
    updater_id: 1,
    rolesUserIds: null,
    userIds: null
  }
...

I have all my models introspecting the data structure from the table and my postgres table definition for roles looks pretty standard:

                                       Table "public.roles"
   Column   |           Type           | Collation | Nullable |              Default              
------------+--------------------------+-----------+----------+-----------------------------------
 id         | bigint                   |           | not null | nextval('roles_id_seq'::regclass)
 name       | character varying(255)   |           |          | 
 created_at | timestamp with time zone |           |          | CURRENT_TIMESTAMP
 updated_at | timestamp with time zone |           |          | CURRENT_TIMESTAMP
 creator_id | bigint                   |           |          | 
 updater_id | bigint                   |           |          | 
...

There are no rolesUserIds or userIds arrays on that table. I haven't reached that part of the OR code, but I'm assuming hasMany is adding it and not removing it when a through is used? Does it matter?

I'm not trying to confuse the issue, but I suspect that having those attributes there and the relations object is empty - maybe there's some issue there?

Feels like I'm close and I'm just missing something small.. I'm going to go through your test code to see how you're testing things for through to see if I can spot something there.

from openrecord.

nibarulabs avatar nibarulabs commented on June 12, 2024

Ok, I found a workaround.

In my user.js I added the following function:

async addRole(role) {
    const RoleUser = require('./role-user');
    const sql = `INSERT INTO roles_users (user_id,role_id) values (${this.id},${role.id})`;
    console.log("sql:", sql)
    const result = await RoleUser.raw(sql);
    await this.roles;
}

This now allows me to do something like:

const role = await Role.find(1);
const user = await User.create({name: 'John'});
user.addRole(role);

I get that it's not ideal and not really 'correct', but it gets the association working for me, albeit with more db hits (I don't need to over optimize at this point and can fix later).

If there are any suggestions as to what else I can try to get the user.roles.add(role) syntax working, then I can try that when it becomes available. For now, this allows me to keep moving forward.

from openrecord.

PhilWaldmann avatar PhilWaldmann commented on June 12, 2024

Yeah, that's not the prettiest solution.
I'll try to rebuild your situation in a test case to see where the problem is. I hope I've time to do so in the next few days.

from openrecord.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.