Comments (4)
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.
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.
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.
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)
- Add documentation or examples to use with ldap HOT 4
- Publish a new release with latest dependency HOT 2
- How can i make raw sql query? HOT 1
- RangeError: Maximum call stack size exceeded HOT 1
- How to select SQL? HOT 2
- select_value as in activerecord HOT 12
- Please update CONTRIBUTING.md HOT 1
- Not returning complete record on create HOT 2
- How can one import/require the ValidationError class? HOT 1
- Help Wanted. Database query builder HOT 2
- Integer keys HOT 4
- Model.find() with empty argument has an unexpected return HOT 2
- Bugs when using offset / limit HOT 5
- How to initialize records from raw rows? HOT 2
- VScode Intellisense for auto generated models HOT 2
- create(data) but get null HOT 2
- Model.create({}) and Model.new({}) autoAttributes = true HOT 11
- ID conversion problem HOT 9
- mysql socketPath HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from openrecord.