could not find a way to get subqueries to work with sequelize so I used raw querying. I am trying to figure out how to get data from associated tables when I do a raw query. This is what I have tried, but it only returns the data from the primary table and nothing from the associated table:
const rawQuery = `select * from (
select distinct on ("patientId") *
from public."Billings"
order by "patientId","createdAt" desc) as "recentPatientBilling"
where balance > 0;`;
const debtors = await sequelize.query(
rawQuery,
{
model: Billing,
mapToModel: true,
nest: true,
raw: true,
include: [{
model: Patient, attributes: ['id']
}]
}
);
The association is:
Billing.associate = function(models) {
Billing.belongsTo(models.User, {
foreignKey: 'employeeId',
as: 'employee'
});
Billing.belongsTo(models.Patient, {
foreignKey: 'patientId',
as: 'patient'
});
};
Any ideas on how I can get this working? (edited)
Did you try putting {as:'patient'} in the include statement?
include: [{
model: Patient,
as:'patient',
attributes: ['id']
}]
Related
const PostPopulateObj = () => ({
path: 'posts',
model: 'Post',
select: 'id order title alias',
...({ match: { published_at: { $ne: null } } })
})
const GroupPopulateObj = {
path: 'groups',
model: 'Group',
select: 'id order label posts groups'
}
module.exports = {
async getNavigationByAlias(ctx) {
const { alias } = ctx.params
const nav = await strapi.query('navigation').find({ alias }, [
{
...GroupPopulateObj,
populate: [PostPopulateObj, {
...GroupPopulateObj,
populate: PostPopulateObj
}]
},
PostPopulateObj
])
return nav.length > 0 ? nav : null
}
};
I have this and using PostgresSQL instead of MongoDB breaks the above query. But my understanding is that it shouldn't break basic queries and only custom queries as shown in the documentations.
https://docs-v3.strapi.io/developer-docs/latest/development/backend-customization.html#queries
https://github.com/strapi/migration-scripts/tree/main/v3-mongodb-v3-sql
I used the scripts and was able to repopulate the db, but like I said I am getting different results, where I am getting some generic default post (converted null post?) instead of 2 specific posts. The post now returned by Postgres seems to not be inside the db, not sure what's going on, but for some reason it's not returning an error.
A little below the section, they mention custom queries and how to use Bookshelf and Mongoose. I used the Mongoose library for custom queries in my understanding, but the above doesn't use Bookshelf or Mongoose at all, so it should work.
I want to return an associated table, but sort those items. Nothing complicated like sorting by an associated table. Just sort the associated elements.
public static async findWith(finder: any): Promise<Race | null> {
const race = await Race.findOne({
where: finder,
include: [
{
model: Rocket,
order: ['id', 'desc']
}
]
})
return race
}
I tried a few options,
order: ['id', 'desc']
order: [['id', 'desc']]
order: [[Rocket, 'id', 'desc']]. // nested
which are all accepted without error, but the sorting isn't applied.
research:
https://github.com/sequelize/sequelize/issues/4553
https://sequelize.org/docs/v6/core-concepts/assocs/
other question
I'm using sequelize-typescript with v6 and postgres.
It is mentioned in sequelize documentation that if you want to apply ORDER clauses to eager loaded models, you must use the top level order option. Therefore, you should write it outside the include option like this
const race = await Race.findOne({
where: finder,
include: [
{
model: Rocket
}
],
order: [[Rocket, 'id', 'desc']]
})
I am trying to count a self relation (followers) in Prisma2 (using PostgreSQL)
Model:
model User {
id String #id #default(cuid())
following User[] #relation(name: "UserFollows")
followers User[] #relation(name: "UserFollows")
}
Query:
const user = await prisma.user.findUnique({
where: { id: userId },
include: {
_count: {
select: { followers: true, following: true },
},
},
});
(using previewFeatures = ["selectRelationCount"]) and getting the following error:
Invalid prisma.user.findUnique() invocation:
Error occurred during query execution: ConnectorError(ConnectorError
{ user_facing_error: None, kind: QueryError(Error { kind: Db, cause:
Some(DbError { severity: "ERROR", parsed_severity: Some(Error), code:
SqlState("42712"), message: "table name "User" specified more than
once", detail: None, hint: None, position: None, where_: None, schema:
None, table: None, column: None, datatype: None, constraint: None,
file: Some("parse_relation.c"), line: Some(423), routine:
Some("checkNameSpaceConflicts") }) }) })
Does anybody have any idea of what I am doing wrong?
This is a known issue with self-relations and we hope to fix it soon. If you want to track this bug, follow this github issue. Please feel free to add a comment to explain your use case/problem over there.
In the meantime, here are some workarounds that you can use:
Find count using nested read
You can use a nested read to return all the records in followers and following and find the length of those arrays to get the count. This seems like the most straightforward way, so long as you're okay with fetching all the followers/following records.
const user = await prisma.user.findUnique({
where: {
id: userId,
},
include: {
followers: true,
following: true,
},
});
let followerCount = user.followers.length;
let followingCount = user.following.length;
Find count using separate count queries.
Alternatively, you can use the count API to find followers and following counts for a certain user.
// number of followers for some user "x" = number of times x.id appaers in "following" relation of other users.
const followerCount = await prisma.user.count({
where: {
following: {
some: {
id: userId,
},
},
},
});
// number of users that user "x" is following = number of times x.id appaers in "followers" relation of other users.
const followingCount = await prisma.user.count({
where: {
followers: {
some: {
id: userId,
},
},
},
});
Change schema to use explicit many-to-many notation
If you're okay with slightly tweaking your schema, you can explicitly define the many-to-many relation table.
model Follows {
follower User #relation("follower", fields: [followerId], references: [id])
followerId String
following User #relation("following", fields: [followingId], references: [id])
followingId String
##id([followerId, followingId])
}
model User {
id String #id #default(cuid())
followers Follows[] #relation("follower")
following Follows[] #relation("following")
}
You should be able to run the count query without issues in this way.
I am using Sails v1.1 -
I created a many-to-many through custom model association following the sails doc here - https://sailsjs.com/documentation/concepts/models-and-orm/associations/through-associations
The PetUser model has two columns pet and user, where each is the respective id. I want to create a unique multi-key index, meaning there cannot be two rows with the same combination of "pet and user". Meaning the second call should succeed, and third call should fail with uniqueness error:
await PetUser.create({ user: 1, pet: 33 }); // should succeed
await PetUser.create({ user: 1, pet: 44 }); // should succeed as user/pet combination is different
await PetUser.create({ user: 1, pet: 33 }); // should fail
I tried adding unique: true to both the owner and pet attribute on PetUser model below, but only the first unique: true gets respected.
So this is my code in myApp/api/models/PetUser.js
module.exports = {
attributes: {
owner: {
model:'user',
unique: true
},
pet: {
model: 'pet',
unique: true
}
}
}
For implementing similar behavior I added a combined attribute and mark it unique. Also, I added beforeCreate and beforeUpdate model hooks on which I generate my combined attribute to check is it unique or not.
const YourModel = {
attributes: {
owner: {
model: 'user',
},
pet: {
model: 'pet',
},
petOwner: {
type: 'string',
unique: true,
}
},
beforeCreate : function(values,cb) {
// TODO get ids from related records or reset to default on missed relation record if you need it
const petId = 35;
const ownerId = 8;
values.petOwner = `${petId}-${ownerId}`;
cb();
},
beforeUpdate : function(values,cb) {
YourModel.beforeCreate(values, cb)
},
};
module.exports = YourModel;
In result when you tries to add the record with the same relations, you will get E_UNIQUE as you expected.
SailsJS with MongoDB adapter not working as expected. I have following relations defined:
Post.js:
module.exports = {
connection: 'mongodb',
attributes: {
title: 'string',
categories: {
collection: 'postCategory',
via: 'posts'
}
}
};
PostCategory.js
module.exports = {
connection: 'mongodb',
attributes: {
name: 'string',
posts: {
collection: 'post',
via: 'categories'
}
}
};
When I query without criteria and populate categories like:
Post.find()
.populate('categories')
.then(...)
it gives me correct result, the document has categories nested.
But when I try to pass criteria it returns no result. e.g.
Post.find({categories: 'food'})
.populate('categories')
.then(...)
Note: I inserted category _id as string (food, travel, etc) in database
Please help
You will not get any results because in categories it will store the _id.
You can get the results by doing the following query.
PostCategory.find({name: 'food'})
.populate('posts')
.then(...)