SailsJS: Many-to-Many association in MySQL - mysql

I've tried to search before posting this question, but no success.
I'm having some troubles trying to understand the best way to achieve a many-to-many association in SailsJS.
Ex:
Hospital has many specialties
Specialty has many hospitals
How do I represent the tables in MySQL so the SailsJS operate on them?
I have an Hospital table and a Specialty table.
Do I have to have an Hospital_Specialty (not sure about if this is the correct tablename to use) table to handle those associations? Like:
Hospital_Specialty
id: int
hospital_id: int
specialty_id: int
I've read the documention but no luck on getting a proper way to achieve what I need.
Thanks in advance

Edit: My initial answer was totally wrong.
The documentation for setting up many <-> many relationships is here:
Many-to-Many | Sails.js
First of all, you'll want to work in Waterline rather than mysql to get this to work properly.
You can set up a many relationship in your model like so:
Specialty.js Model
module.exports = {
attributes: {
hospitals:{
collection: 'hospital',
via: 'specialties'
}
}
}
Hospital.js Model
module.exports = {
attributes: {
specialties:{
collection: 'specialty',
via: 'hospitals'
}
}
}
This will set up the required intermediate tables.
Up to you if you want to use plurals or not.

Related

Prisma generates database tables with uppercase in mysql. Is there any way to generate tables only in lowercase?

Prisma generates database tables with uppercase in MySQL. Is there any way to generate tables only in lower case?
At the moment you have to do that manually with #map and ##map.
You can read more about this here.
Here an example:
model PostInCategories {
category Category #map(name: "category_id")
post Post #map(name: "post_id")
##unique([post, category], name: "post_id_category_id_unique")
##map(name: "post_in_categories")
}

Getting another table's string via it's id

I have two tables, but since I can't format them here, you can check the table on this imgur img
The wildcard roles includes strings like department.* or blogger.department.*
I'm trying to create a role/permission inheritance system but i'm stuck on creating a many-to-many relationship
I've tried to use eloquent but since "wildcards" can't be/aren't a model , I can't use eloquent. So I tried to use the DB Facade but I'm not very good with SQL/DB so I've honestly got no clue on how to do this.
Here's what I need
Get the role ID (eg: admin = 1)
Get all wildcard_roles from the table role_wildcards by the Role ID
Return a collection of all the wildcard strings
Notes
It doesn't have to use eloquent, it can use the DB facade
It was actually quite simple after playing around
DB::table('role_wildcards')->where('parent_role_id', $this->id)->get();
Thought it'd be much more complicated
First of you create a relationship from role_wildcards to roles
public function role()
{
return $this->belongsTo('App\roles','parent_role_id','id');
}
then you use relationship like below
DB::table('role_wildcards')->with('role')->whereParentRoleId($this->id)->get();

Can we fetch a table models from MySql using Nodejs ORM2 without defining all the properties/fields?

I would like to know if we can fetch a table model from MySql from Database, without defining all the properties.
I'm using Node.js ORM2
const orm = require('orm');
db.define('users', {
// donot want to specify all the properties/fields
});
Above users table has already been created in DB, so I just want to get that model and not create/specify all the properties in it.
We can do this with other ORM's like Objection.js - but can't change my current implementation.
Thanks in advance,
Unfortunately, you must define properties/fields while you are defining your Models. There aren't any other options.

Sequelize create object with associations

I'm trying to save sequelize models with their associations. All the associations are one to one. Retrieving models with associations from the database works just fine but inserting them is another matter and the documentation is just making me more confused.
Here's my insert method:
models
.radcheck
.create(user, {
include: [{model: models.skraningar}, {model: models.radusergroup}, {model: models.radippool}]
})
.then(success, error);
I've seen so many ways to do this both in the documentation and here on stackoverflow and none of them make sense to me so far. Anyone care to clear things up for me?
I always save a model with association by it's foreign key
e.g
models.user.create({ name : 'test', usergroup_id : 1 });

How to correctly define one-to-many relations with existing joint table using node Sequelize

I am using Sequelize, but since I also have other servers running other than node.js, I need to let them share the database. So when defining one-to-many relation, I need to let Sequelize use the old existing jointTable.
I write my definition of the association as below, where pid is the primary key of presentations:
this.courses.hasMany(this.presentations,
{as : 'Presentations',
foreignKey : 'cid',
joinTableName : 'course_presentations'
});
Or this one:
this.courses.hasMany(this.presentations,
{as : 'Presentations',
foreignKey : 'pid',
joinTableName : 'course_presentations'
});
I am using the below codes to retrieve the associated presentations:
app.get('/courses/presentations/:cid', function (req, res){
var cid = req.params.cid;
app.models.courses.find({where: {cid:cid}}).success(function(course){
course.getPresentations().success(function(presentations){
res.send(presentations);
});
});
});
The previous one will tell me there is no cid in 'presentations' table.
The latter one will give something like this:
Executing: SELECT * FROM `courses`;
Executing: SELECT * FROM `courses` WHERE `courses`.`cid`='11' LIMIT 1;
Executing: SELECT * FROM `presentations` WHERE `presentations`.`pid`=11;
Check carefully, I found that everytime, it is always using the cid value to query for presentations, which means only when they happen to share the same id value, something can be returned. And even for those, it is not correct.
I am strongly suspecting, Sequelize are not using the joinTable I specified, instead, it is still trying to find the foreign keys in the original two tables. It is viewing pid as the reference of cid in presentations, which causes this problem.
So I am wondering how to correctly set up the junction table so that the two of them can use the foreign keys correctly.
jointTableName : 'course_presentations'
should be (without a t)
joinTableName : 'course_presentations'
Actually AFAIK - this kind of relation is not "pure" one-to-many.
You have one course can have many entries in course_presenation table, and course_presentation have one-to-one relation with presentation. If so, just define that kind of associations in model.