Here is the sequelize call I'm making to count favorites along with my model:
Model.findAll({
group: [ 'model.id', 'favorites.id' ],
attributes: [
'*',
[ Sequelize.fn('count', Sequelize.col('favorites.id')), 'favorites_count' ]
],
include: [
{ attributes: [], model: Favorite },
]
});
Here's the query it produces:
SELECT
model.id,
model.*,
count(favorites.id) AS favorites_count,
favorites.id AS favorites.id # Invalid!
FROM models AS model ...
Here's the error
ERROR: syntax error at or near "."
LINE 1: SELECT model.*, favorites.id AS favorites.id, ...
^
Because favorites.id is not a valid AS alias. Why does Sequelize produce this
invalid alias and how do I prevent it?
You cannot prevent it - sequelize needs to alias the column. The query becomes invalid because you have set quoteIdentifiers to false. The query sequelize wants is "favorites"."id" AS "favorites.id"
Why is sequelize doing such a "stupid" thing you might say. Well lets take an example:
SELECT "model"."id", "favorites"."id" ...
This produces the following result set:
[{ id: 42 }]
Because the two columns have the same name, they override each other so only one of the columns is returned. So we need to alias the favorites column so we get
[{ id: 42, 'favorites.id': 87 }]
Related
I have the following findAll query:
let consumQuery = Manager.consumption.findAndCountAll({
where: query, attributes: ['ml', 'price', 'total', 'consumption_end', 'bar_id', 'sync', 'visit.device_id',
'visit.person.document','visit.person.person_id','visit.person.name','visit.person.surname',
'keg.product_id','keg.product.sku','keg.product.name'],
include: [
{
model: Manager.visit,
attributes: [],
include: [{
model: Manager.person,
attributes: [],
raw: true
}],
},
{
model: Manager.keg,
attributes: [],
include: [
{
model: Manager.product,
required: true,
attributes: []
}
]
}],
limit: PAGE_LIMIT_SIZE,
offset: offset,
raw: true,
order: [['consumption_start', 'ASC']]
}).then(({count, rows}) => {
I'm trying to rename the column keg.product.name to beer.
I've tried :
...'keg.product_id','keg.product.sku',['keg.product.name','beer']
But I got the error:
"column \"keg.product.name\" does not exist"
The sequelize output the following SQL:
sql: `SELECT "consumption"."ml", "consumption"."price", "consumption"."total", "consumption"."consumption_end", "consumption"."bar_id", "consumption"."sync", "visit"."device_id", "visit.person"."document", "visit.person"."person_id", "visit.person"."name", "visit.person"."surname", "keg"."product_id", "keg.product"."sku", "keg.product.name" AS "beer" FROM "public"."consumption" AS "consumption" LEFT OUTER JOIN "public"."visit" AS "visit" ON "consumption"."visit_id" = "visit"."visit_id" LEFT OUTER JOIN "public"."person" AS "visit.person" ON "visit"."person_id" = "visit.person"."person_id" LEFT OUTER JOIN "public"."keg" AS "keg" ON "consumption"."keg_id" = "keg"."keg_id" INNER JOIN "public"."product" AS "keg.product" ON "keg"."product_id" = "keg.product"."product_id" WHERE ("consumption"."bar_id" = '248' AND "consumption"."sync" = 'true' AND "consumption"."consumption_start" >= '2020-02-16 20:03:32.000 -03:00' AND "consumption"."consumption_end" <= '2020-09-17 20:03:32.000 -03:00') ORDER BY "consumption"."consumption_start" ASC LIMIT 500 OFFSET 0;},
The result is "keg.product.name" AS "beer", but I think should be something like that: "keg.product"."name" AS "beer"
Any ideia?
To rename an attribute you have to make it an array like [name_in_db, name_you_want].
Therefore your attributes value should be as it follows
attributes: ['ml', 'price', 'total', 'consumption_end', 'bar_id', 'sync', 'visit.device_id', visit.person.document', 'visit.person.person_id', 'visit.person.name', 'visit.person.surname', keg.product_id', 'keg.product.sku', **['keg.product.name', 'beer']**]
Source: https://sequelize.org/api/v6/class/src/model.js~model#static-method-findAll
"To rename an attribute, you can pass an array, with two elements - the first is the name of the attribute in the DB [...], and the second is the name you want the attribute to have in the returned instance"
I have events collection inserted with a below record in ARANGODB. ( I am new to Arango )
INSERT {
"source": "ABC",
"target": "ZYX",
"tranno": "ABCDEF",
"type": "REST",
"attributes" : { "myID" : "12345"}
} INTO events
But trying to create full text index on attributes, resulting in error as given below. It would be great if you could help with this.
events.createIndex ({ type: "fulltext", fields: [ "attributes" ], minLength: 3 })
Query: AQL: syntax error, unexpected identifier near 'events.createIndex ({ type: "ful...' at position 1:1 (while parsing)
Unlike SQL, AQL is a language used for data selection and data manipulation.
It is not a data definition language, so you can't use AQL to create indexes.
In order to create an index, please use ArangoDB's web interface (Collections => target collection => Indexes => "+" icon) or the ArangoShell. The ArangoShell is a separate executable that is shipped with all ArangoDB packages.
In the ArangoShell you can use the command
db.events.createIndex ({ type: "fulltext", fields: [ "attributes" ], minLength: 3 })
to create the index.
I can able to search the case by company name
var mySearch = search.create({
type: search.Type.SUPPORT_CASE,
columns: [{
name: 'title'
}, {
name: 'company'
}],
filters: [{
name: 'company',
operator: 'is',
values: 'Test'
}]
});
return mySearch.run({
ld: mySearch.id
}).getRange({
start: 0,
end: 1000
});
But I am not able to search case by company id.
companyId is 115
Below are not working
i)
filters: [{
name: 'company',
operator: 'is',
values: 115
}]
ii)
filters: [{
name: 'companyid',
operator: 'is',
values: 115
}]
According to the Case schema company is a Text filter, meaning you would have to provide it with the precise Name of the company, not the internal ID.
Instead you may want to use the customer.internalid joined filter to provide the internal ID. Also, Internal ID fields are nearly always Select fields, meaning they do not accept the is operator, but instead require the anyof or noneof operator.
You can find the valid operators by field type on the Help page titled Search Operators
First, you can try this :
var supportcaseSearchObj = search.create({
type: "supportcase",
filters:
[
["company.internalid","anyof","100"]
],
columns:
[
search.createColumn({
name: "casenumber",
sort: search.Sort.ASC
}),
"title",
"company",
"contact",
"stage",
"status",
"profile",
"startdate",
"createddate",
"category",
"assigned",
"priority"
]
});
Second : how did I get this ? The answer is hint that will make your life easier :
Install the "NetSuite Saved Search Code Export" chrome plugin.
In Netsuite UI, create your saved search (it is always easier that doing it in code).
After saving the search, open it again for edition.
At the top right corner (near list, search menu in the netsuite page), you will see a link "Export as script" : click on it and you will get your code ;)
If you can not install the chrome plugin :
In Netsuite UI, create your saved search (it is always easier that doing it in code).
In your code, load your saved search
Add a log.debug to show the [loadedesearchVar].filters
You can then copy what you will see in the log to use it as your search filters.
Good luck!
Let's say I have the following document
{
name: "Johnny Doe",
age: "55",
organizations: [
{name: "Org A",
members: 22
},
{name: "Org B",
members: 25
}]
}
And lets say I have a virtual set up for another schema
SomeSchema.virtual('organizations'){
ref:"Person", //the collection to reference
localField:"organization_name", //the field in this Schema
foreignField: [???] //I want to access Person.organizations[].name
}
How do I specify the field I want in localField/foreignField? I have tried "organizations.$.name" like in MongoDB but it doesn't work, it returns an empty array.
I found the answer. Just use field_array.field_of_array_object or in my example organizations.name. It doesn't matter if its in an array, just use the . notation.
I'm using this query:
db.Spectrum.findAll({
attributes: ['title', [db.Sequelize.fn('SUM', db.Sequelize.col('quantity') ), 'count']],
include: [
{model: db.Varietal,
attributes : ['specID', 'id'],
include : [
{ model : db.Wine,
required: false,
attributes : ['varietal_id', 'id'],
where : {restaurant_id : restaurant_id,
owner_id : null} }
],
}
],
group : ['specID']
})
Which gives me the correct results, the problem is because it ands the where clause to the left join it runs really slow (about 2 seconds).
The raw query of the left join looks like this.
LEFT OUTER JOIN `wines` AS `Varietals.Wines` ON `Varietals`.`id` = `Varietals.Wines`.`varietal_id` AND `Varietals.Wines`.`restaurant_id` = 16 AND `Varietals.Wines`.`owner_id` IS NULL
I would like the query to have the left join and a where clause like this..
LEFT OUTER JOIN `wines` AS `Varietals.Wines` ON `Varietals`.`id` = `Varietals.Wines`.`varietal_id`
WHERE `Varietals.Wines`.`restaurant_id` = 16 AND `Varietals.Wines`.`owner_id` IS NULL
But everything I try puts 'Spectrum' before it and then doesn't find the row. How is this suppose to be done? Or am I going to have to just use a raw query?
include.where will always add the condition to the join.
However in Sequelize v3.13.0 support for special nested keys were added.
It's now possible to add a where object to the toplevel object containing dot seperated keys wrapped in $ to specify it being a nested field.
Applying it to your code:
db.Spectrum.findAll({
include: [
{
model: db.Varietal,
include : [
{
model : db.Wine
}
]
}
],
where: {
'$Varietals.Wines.restaurant_id$': restaurant_id,
'$Varietals.Wines.owner_id$': null
},
group : ['specID']
});
(I removed a few statements to focus on the important part)
Include array
include = [{
model: db.caregiverCategory,
as: 'categories',
attributes: ['name'],
required: true,
attributes: ['name'],
include: [{
model: db.category,
as: 'category',
required: true
}]
}]
Where Clause
where[Op.or] = [{
'$categories.name$': { [Op.like]: '%' + query.category_name + '%' }
}, {
'$categories.category.name$': { [Op.like]: '%' + query.category_name + '%' }
}]
You need to add all fields like name (which are required in or) in attributes: ['name'] otherwise it will give unknowm columns error.