I have two tables I am querying with bookshelfjs using 'withRelated'. How do I sort the results by a column in the related table? For example I want to order all the results by rankingLinks.id
.fetchAll({withRelated: ['tickerSymbolLinks.tickerSymbol', 'rankingLinks', 'logoLinks.logo']})
Seems simple but banging my head against the wall on this one...
You can actually order stuff by withRelated, however, it will only order the result within this specific property. I.e.:
new Participant().query(function(qb) {
qb.orderBy('participants.id', 'desc'); // You don't have access to any "withRelated" models
}).fetchAll({
withRelated: [{
'courses': function(qb) {
qb.orderBy('courses.id', 'desc');
}
}]
}).then(function(result) {
res.json(result.toJSON());
});
This code returns all participants, order by their id (descending). Within this result, I order courses by courses.id desc. The result is as follows:
[
{
"id": 3,
"name": "Susan Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"courses": [
{
"id": 3,
"course_name": "Advanced JavaScript",
"duration": 80,
"_pivot_participant_id": 3,
"_pivot_course_id": 3,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 1,
"course_name": "iOS development",
"duration": 120,
"_pivot_participant_id": 3,
"_pivot_course_id": 1,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
},
{
"id": 2,
"name": "Richard Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"courses": [
{
"id": 2,
"course_name": "Android development",
"duration": 60,
"_pivot_participant_id": 2,
"_pivot_course_id": 2,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 1,
"course_name": "iOS development",
"duration": 120,
"_pivot_participant_id": 2,
"_pivot_course_id": 1,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
},
{
"id": 1,
"name": "John Doe",
"created_at": 1429685077495,
"updated_at": 1429685077495,
"courses": [
{
"id": 3,
"course_name": "Advanced JavaScript",
"duration": 80,
"_pivot_participant_id": 1,
"_pivot_course_id": 3,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 1,
"course_name": "iOS development",
"duration": 120,
"_pivot_participant_id": 1,
"_pivot_course_id": 1,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
}
]
Now if I'd like to order the results by courses, I'd have to change the query a bit. I.e.:
new Course().query(function(qb) {
qb.orderBy('courses.id', 'desc');
}).fetchAll({
withRelated: ['participants']
}).then(function(result) {
res.json(result.toJSON());
});
This produces a slightly different result, but with desired ordering:
[
{
"id": 3,
"course_name": "Advanced JavaScript",
"duration": 80,
"participants": [
{
"id": 1,
"name": "John Doe",
"created_at": 1429685077495,
"updated_at": 1429685077495,
"_pivot_course_id": 3,
"_pivot_participant_id": 1,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 3,
"name": "Susan Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"_pivot_course_id": 3,
"_pivot_participant_id": 3,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
},
{
"id": 2,
"course_name": "Android development",
"duration": 60,
"participants": [
{
"id": 2,
"name": "Richard Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"_pivot_course_id": 2,
"_pivot_participant_id": 2,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
},
{
"id": 1,
"course_name": "iOS development",
"duration": 120,
"participants": [
{
"id": 1,
"name": "John Doe",
"created_at": 1429685077495,
"updated_at": 1429685077495,
"_pivot_course_id": 1,
"_pivot_participant_id": 1,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 2,
"name": "Richard Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"_pivot_course_id": 1,
"_pivot_participant_id": 2,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
},
{
"id": 3,
"name": "Susan Doe",
"created_at": 1429685077496,
"updated_at": 1429685077496,
"_pivot_course_id": 1,
"_pivot_participant_id": 3,
"_pivot_level": null,
"_pivot_grade": null,
"_pivot_date_of_attendance": null
}
]
}
]
I hope this helps a bit!
Related
I am using retool, which uses AlaSQL to query JSON, trying to get the value from a field that has multiple items; I only want to return the value of the field stock for the key with the latest created_at date.
select tracked_products
from {{ean.data}}
where created_at=(select MAX(created_at) from {{ean.data}});
I can actually get to the fields I want to like this but I do not understand how to filter further
select tracked_products
from {{ean.data}}
where price < 40
result from above query in plain text
[
{
"id": 172165913,
"offer_data_id": 2208536,
"stock": 2,
"sold": 0,
"revenue": "0.00",
"price": 39.99,
"status": "success",
"created_at": "2022-12-16T12:12:26.000000Z",
"updated_at": "2022-12-16T12:12:26.000000Z"
},
{
"id": 173409443,
"offer_data_id": 2208536,
"stock": 2,
"sold": 0,
"revenue": "0.00",
"price": 34.99,
"status": "success",
"created_at": "2022-12-17T03:15:58.000000Z",
"updated_at": "2022-12-17T03:15:58.000000Z"
},
{
"id": 174659591,
"offer_data_id": 2208536,
"stock": 2,
"sold": 0,
"revenue": "0.00",
"price": 34.99,
"status": "success",
"created_at": "2022-12-18T16:22:45.000000Z",
"updated_at": "2022-12-18T16:22:45.000000Z"
},
{
"id": 175895075,
"offer_data_id": 2208536,
"stock": 2,
"sold": 0,
"revenue": "0.00",
"price": 34.99,
"status": "success",
"created_at": "2022-12-19T03:18:53.000000Z",
"updated_at": "2022-12-19T03:18:53.000000Z"
},
{
"id": 177134025,
"offer_data_id": 2208536,
"stock": 2,
"sold": 0,
"revenue": "0.00",
"price": 34.99,
"status": "success",
"created_at": "2022-12-20T15:35:48.000000Z",
"updated_at": "2022-12-20T15:35:48.000000Z"
},
{
"id": 178391290,
"offer_data_id": 2208536,
"stock": 2,
"sold": 0,
"revenue": "0.00",
"price": 34.99,
"status": "success",
"created_at": "2022-12-21T03:09:22.000000Z",
"updated_at": "2022-12-21T03:09:22.000000Z"
},
{
"id": 179654380,
"offer_data_id": 2208536,
"stock": 1,
"sold": 1,
"revenue": "34.99",
"price": 34.99,
"status": "success",
"created_at": "2022-12-22T03:13:50.000000Z",
"updated_at": "2022-12-22T03:13:50.000000Z"
},
{
"id": 180918092,
"offer_data_id": 2208536,
"stock": 1,
"sold": 0,
"revenue": "0.00",
"price": 34.99,
"status": "success",
"created_at": "2022-12-23T03:19:42.000000Z",
"updated_at": "2022-12-23T03:19:42.000000Z"
}
]
try this :
SELECT DISTINCT
(array_agg(j->>'stock') OVER (PARTITION BY tracked_products ORDER BY j->>'created_at' DESC)[1] AS stock
FROM ean.data
CROSS JOIN LATERAL jsonb_path_query(tracked_products :: jsonb, '$[*]') AS j
WHERE price < 40
This query relies on a text sorting on created_at which should be compliant with the actual date format of this text field.
jsonb_path_query is used instead of json_array_elements so that to avoid an error when tracked_products is not a json of type array.
see dbfiddle
Refer to the manual for more info about json manipulation
Issue
When logged in as a buyer, I am unable to see a product I should have access to. Issuing this request, /v1/me/products while passing a token for user buyer01, I see this response:
{
"Meta": {
"Facets": [],
"Page": 1,
"PageSize": 20,
"TotalCount": 0,
"TotalPages": 0,
"ItemRange": [
1,
0
],
"NextPageKey": null
},
"Items": []
}
I expect to see the product SHIRT listed in the Items array element.
Visibility rules
I've worked through the Visibility Rules Checklist and I believe all conditions have been met:
Product.Active = true
Catalog exists where:
Catalog.Active = true
Buyer is assigned via CatalogAssignment
Product is assigned via ProductCatalogAssignment
One of the following is true:
CatalogAssignment.ViewAllProducts = true
Product is active
{{baseUrl}}/v1/products returns this response:
{
"Meta": {
"Facets": [],
"Page": 1,
"PageSize": 20,
"TotalCount": 1,
"TotalPages": 1,
"ItemRange": [
1,
1
],
"NextPageKey": null
},
"Items": [
{
"OwnerID": "xxxxxxxxxxxxxxxx",
"DefaultPriceScheduleID": "SHIRT_PRICE",
"AutoForward": false,
"ID": "SHIRT",
"Name": "Cotton T-Shirt",
"Description": "A plain white, cotton shirt for everyday use.",
"QuantityMultiplier": 1,
"ShipWeight": null,
"ShipHeight": null,
"ShipWidth": null,
"ShipLength": null,
"Active": true,
"SpecCount": 0,
"VariantCount": 0,
"ShipFromAddressID": null,
"Inventory": null,
"DefaultSupplierID": null,
"AllSuppliersCanSell": false,
"Returnable": false,
"xp": null
}
]
}
Catalog is active
{{baseUrl}}/v1/catalogs returns
{
"Meta": {
"Page": 1,
"PageSize": 20,
"TotalCount": 1,
"TotalPages": 1,
"ItemRange": [
1,
1
],
"NextPageKey": null
},
"Items": [
{
"ID": "BUYER_ORGANIZATION",
"OwnerID": "xxxxxxxxxxxxxxxx",
"Name": "Example Buyer",
"Description": "Default catalog for Example Buyer",
"Active": true,
"CategoryCount": 0,
"xp": null
}
]
}
Buyer is assigned
Buyer user in organization
{{baseUrl}}/v1/me (with buyer01 cookie) returns:
{
"Buyer": {
"ID": "BUYER_ORGANIZATION",
"DefaultCatalogID": "BUYER_ORGANIZATION"
},
"Supplier": null,
"Seller": {
"ID": "xxxxxxxxxxxxxxxx"
},
"ID": "BUYER_USER",
"CompanyID": "BUYER_ORGANIZATION",
"Username": "buyer01",
"Password": null,
"FirstName": "Buyer",
"LastName": "User",
"Email": "buyer#email.com",
"Phone": null,
"TermsAccepted": null,
"Active": true,
"xp": null,
"AvailableRoles": [
"MeAdmin",
"PasswordReset",
"Shopper"
],
"Locale": null,
"DateCreated": "2022-12-21T00:43:53.507+00:00",
"PasswordLastSetDate": "2022-12-21T00:43:53.543+00:00"
}
Buyer organization is assigned catalog
{{baseUrl}}/v1/catalogs/assignments returns
{
"Meta": {
"Page": 1,
"PageSize": 20,
"TotalCount": 1,
"TotalPages": 1,
"ItemRange": [
1,
1
],
"NextPageKey": null
},
"Items": [
{
"CatalogID": "BUYER_ORGANIZATION",
"BuyerID": "BUYER_ORGANIZATION",
"ViewAllCategories": true,
"ViewAllProducts": true
}
]
}
Product is assigned
This may be the issue. When I make the documented assignment:
{{baseUrl}}/v1/catalogs/productassignments
{
"CatalogID": "BUYER_ORGANIZATION",
"ProductID": "SHIRT"
}
I receive a 200 response that does not show any assignment has occurred:
{
"Meta": {
"Page": 1,
"PageSize": 20,
"TotalCount": 0,
"TotalPages": 0,
"ItemRange": [
1,
0
],
"NextPageKey": null
},
"Items": []
}
Note that the above request, as all other steps, is taken from the Getting Started walkthrough. This step is provided under Making Your Product Visible.
This is confirmed by this request:
{{baseUrl}}/v1/products/assignments, which returns no products.
{
"Meta": {
"Page": 1,
"PageSize": 20,
"TotalCount": 0,
"TotalPages": 0,
"ItemRange": [
1,
0
],
"NextPageKey": null
},
"Items": []
}
View all products is true
{{baseUrl}}/v1/catalogs/assignments returns
{
"Meta": {
"Page": 1,
"PageSize": 20,
"TotalCount": 1,
"TotalPages": 1,
"ItemRange": [
1,
1
],
"NextPageKey": null
},
"Items": [
{
"CatalogID": "BUYER_ORGANIZATION",
"BuyerID": "BUYER_ORGANIZATION",
"ViewAllCategories": true,
"ViewAllProducts": true
}
]
}
The issue was that I was making a GET request instead of a POST when doing this:
{{baseUrl}}/v1/catalogs/productassignments
When issuing a POST, the service returns a 204 No Content.
It is worth noting that the tutorial is a bit out of date--the current Product API documentation omits the above call, and instead has /products/assignments here. This method will return a 405 Method Not Allowed an error if doing a GET, which makes this issue easier to identify.
Now when I view /me/products as buyer01 I see this:
{
"Meta": {
"Facets": [],
"Page": 1,
"PageSize": 20,
"TotalCount": 1,
"TotalPages": 1,
"ItemRange": [
1,
1
],
"NextPageKey": null
},
"Items": [
{
"PriceSchedule": {
"OwnerID": "xxxxxxxxxxxxxxxx",
"ID": "SHIRT_PRICE",
"Name": "Cotton T-Shirt Price",
"ApplyTax": false,
"ApplyShipping": false,
"MinQuantity": 1,
"MaxQuantity": null,
"UseCumulativeQuantity": false,
"RestrictedQuantity": false,
"PriceBreaks": [
{
"Quantity": 1,
"Price": 10.0,
"SalePrice": null
}
],
"Currency": null,
"SaleStart": null,
"SaleEnd": null,
"IsOnSale": false,
"xp": null
},
"ID": "SHIRT",
"Name": "Cotton T-Shirt",
"Description": "A plain white, cotton shirt for everyday use.",
"QuantityMultiplier": 1,
"ShipWeight": null,
"ShipHeight": null,
"ShipWidth": null,
"ShipLength": null,
"Active": true,
"SpecCount": 0,
"VariantCount": 0,
"ShipFromAddressID": null,
"Inventory": null,
"DefaultSupplierID": null,
"AllSuppliersCanSell": false,
"Returnable": false,
"xp": null
}
]
}
I have three tables posts, comments, and users. posts table includes two types of posts, question and answer. Comments are made on questions and answers. My purpose is to get a single question with comments on it, answers on it and comments on that answers. Also I need username from the users table as author on each question, answer and comment I fetch. I am using Postgres 9.5, and making use of json_agg() function.
While example output I need should be something similar to the first following, I get repeated entries.
What is the thing I am missing here? Correct group by clauses may be. Or the subquery to gather the answers with their comments is not the way to do it. When I comment out the left join from posts on the comments table I get desired results without comments on questions. Also when I cancel the left join containing the subquery I get non-repetative results as I expect, and again that is not complete dataset I desire. These are things I have collected so far to solve my issue.
What I need:
[
{
"post_id": "10",
"created_at": "2016-05-10T00:16:54.469Z",
"post_type": "question",
"post_title": "qwerty",
"post_text": "asdasd asda sdasd",
"post_author_id": 1,
"author": "isikfsc",
"parent_post_id": null,
"is_accepted": null,
"acceptor_id": null,
"answers": [
{
"post_id": 17,
"created_at": "2016-05-10T04:58:56.350229",
"post_type": "answer",
"post_title": null,
"post_text": "222asda dasdad asdada",
"post_author_id": 1,
"author": "isikfsc",
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 5,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"author": "isikfsc",
"parent_post_id": 17
},
{
"id": 8,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"author": "isikfsc",
"parent_post_id": 17
}
]
},
{
"post_id": 14,
"created_at": "2016-05-10T04:19:19.005556",
"post_type": "answer",
"post_title": null,
"post_text": "asdasdasdasd",
"post_author_id": 1,
"author": "isikfsc",
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 2,
"created_at": "2016-05-10T05:25:34.671008",
"text": "qeqweqwe",
"author_id": 1,
"author": "isikfsc",
"parent_post_id": 14
}
]
}
],
"comments": [
{
"id": 1,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"author": "isikfsc",
"parent_post_id": 10
},
{
"id": 4,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"author": "isikfsc",
"parent_post_id": 10
}
]
}
]
My query is:
SELECT
q.*,
json_agg(ac.*) AS answers,
json_agg(c.*) AS comments --comments on posts of post_id questions
FROM posts q
LEFT JOIN
(
SELECT
a.*,
json_agg(c.*) AS comments -- comments on posts of post_id answers
FROM posts a
LEFT JOIN comments c
ON a.post_id = c.parent_post_id
GROUP BY a.post_id
) ac
ON q.post_id = ac.parent_post_id
LEFT JOIN comments c
ON q.post_id = c.parent_post_id
WHERE q.post_id = 10
GROUP BY q.post_id
What I get:
[
{
"post_id": "10",
"created_at": "2016-05-10T00:16:54.469Z",
"post_type": "question",
"post_title": "qwerty",
"post_text": "asdasd asda sdasd",
"post_author_id": 1,
"parent_post_id": null,
"is_accepted": null,
"acceptor_id": null,
"answers": [
{
"post_id": 17,
"created_at": "2016-05-10T04:58:56.350229",
"post_type": "answer",
"post_title": null,
"post_text": "222asda dasdad asdada",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 5,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"parent_post_id": 17
},
{
"id": 8,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"parent_post_id": 17
}
]
},
{
"post_id": 17,
"created_at": "2016-05-10T04:58:56.350229",
"post_type": "answer",
"post_title": null,
"post_text": "222asda dasdad asdada",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 5,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"parent_post_id": 17
},
{
"id": 8,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"parent_post_id": 17
}
]
},
{
"post_id": 17,
"created_at": "2016-05-10T04:58:56.350229",
"post_type": "answer",
"post_title": null,
"post_text": "222asda dasdad asdada",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 5,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"parent_post_id": 17
},
{
"id": 8,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"parent_post_id": 17
}
]
},
{
"post_id": 17,
"created_at": "2016-05-10T04:58:56.350229",
"post_type": "answer",
"post_title": null,
"post_text": "222asda dasdad asdada",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 5,
"created_at": "2016-05-10T10:56:30.220128",
"text": "qweqwe",
"author_id": 1,
"parent_post_id": 17
},
{
"id": 8,
"created_at": "2016-05-10T11:00:00.182991",
"text": "sasasd",
"author_id": 1,
"parent_post_id": 17
}
]
},
{
"post_id": 14,
"created_at": "2016-05-10T04:19:19.005556",
"post_type": "answer",
"post_title": null,
"post_text": "asdasdasdasd",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 2,
"created_at": "2016-05-10T05:25:34.671008",
"text": "qeqweqwe",
"author_id": 1,
"parent_post_id": 14
}
]
},
{
"post_id": 14,
"created_at": "2016-05-10T04:19:19.005556",
"post_type": "answer",
"post_title": null,
"post_text": "asdasdasdasd",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 2,
"created_at": "2016-05-10T05:25:34.671008",
"text": "qeqweqwe",
"author_id": 1,
"parent_post_id": 14
}
]
},
{
"post_id": 14,
"created_at": "2016-05-10T04:19:19.005556",
"post_type": "answer",
"post_title": null,
"post_text": "asdasdasdasd",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 2,
"created_at": "2016-05-10T05:25:34.671008",
"text": "qeqweqwe",
"author_id": 1,
"parent_post_id": 14
}
]
},
{
"post_id": 14,
"created_at": "2016-05-10T04:19:19.005556",
"post_type": "answer",
"post_title": null,
"post_text": "asdasdasdasd",
"post_author_id": 1,
"parent_post_id": 10,
"is_accepted": null,
"acceptor_id": null,
"comments": [
{
"id": 2,
"created_at": "2016-05-10T05:25:34.671008",
"text": "qeqweqwe",
"author_id": 1,
"parent_post_id": 14
}
]
}
],
"comments": [
{
"id": 1,
"created_at": "2016-05-10T05:25:28.200327",
"text": "asadasdad",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 4,
"created_at": "2016-05-10T10:25:23.381177",
"text": "werwer",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 1,
"created_at": "2016-05-10T05:25:28.200327",
"text": "asadasdad",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 4,
"created_at": "2016-05-10T10:25:23.381177",
"text": "werwer",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 1,
"created_at": "2016-05-10T05:25:28.200327",
"text": "asadasdad",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 4,
"created_at": "2016-05-10T10:25:23.381177",
"text": "werwer",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 1,
"created_at": "2016-05-10T05:25:28.200327",
"text": "asadasdad",
"author_id": 1,
"parent_post_id": 10
},
{
"id": 4,
"created_at": "2016-05-10T10:25:23.381177",
"text": "werwer",
"author_id": 1,
"parent_post_id": 10
}
]
}
]
Grouping happens once all of the parties have been joined, so aggregates will depend on the resulting cardinality. Joining posts with answers AND comments causes a full join between them, duplicating all values. They need to be separated and performed individually, one way you can do it is the following:
SELECT
q.*,
(SELECT json_agg(ac.*)
FROM (
SELECT a.*, json_agg(c.*) AS comments
FROM posts a
LEFT JOIN comments c ON (a.post_id = c.parent_post_id)
WHERE a.parent_post_id = q.post_id
GROUP BY a.post_id
) ac
) AS answers,
json_agg(c.*) AS comments --comments on posts of post_id questions
FROM posts q
LEFT JOIN comments c ON (q.post_id = c.parent_post_id)
WHERE q.post_id = 10
GROUP BY q.post_id;
Alternatively:
SELECT q.*, qa.answers, qc.comments
FROM posts q
LEFT JOIN (
SELECT ac.parent_post_id, json_agg(ac.*) AS answers
FROM (
SELECT ac.*, json_agg(c.*) AS comments
FROM posts ac
LEFT JOIN comments c ON (c.parent_post_id = ac.post_id)
GROUP BY ac.post_id
) ac
GROUP BY ac.parent_post_id
) qa ON (qa.parent_post_id = q.post_id)
LEFT JOIN (
SELECT c.parent_post_id, json_agg(c.*) AS comments
FROM comments c
GROUP BY c.parent_post_id
) qc ON (qc.parent_post_id = q.post_id)
WHERE q.post_id = 10;
I am posting the following Order to the shopify api Order endpoint. The Order shows up in the shop and everything works as it should, except that the stock quantity of the variants in orders placed via the API are not decreased automatically by shopify. When I place an order within the admin console, they are decreased automatically. Shopify inventory tracking is turned on for the products. Any ideas would be greatly appreciated.
{
"order": {
"email": "someName#yahoo.com",
"financial_status": "paid",
"fulfillment_status": null,
"send_receipt": true,
"send_fulfillment_receipt": true,
"note": "Created by someName",
"line_items": [
{
"variant_id": 21718275463,
"quantity": 1,
"price": 99,
"requires_shipping": true,
"product_id": 6820646151
},
{
"variant_id": 21717700871,
"quantity": 1,
"price": 1000,
"requires_shipping": true,
"product_id": 6820646151
},
{
"variant_id": 21717690055,
"quantity": 1,
"price": 555,
"requires_shipping": true,
"product_id": 6821668807
}
],
"processing_method": "offsite",
"shipping_address": {
"first_name": "Chris",
"address1": "111 Love Road",
"phone": "9999999999",
"city": "St. Louis",
"zip": "63123",
"province": "MO",
"country": "United States",
"last_name": "Becker",
"name": "Chris Becker",
"country_code": "US",
"province_code": "MO"
},
"source_name": "someName",
"taxes_included": false,
"shipping_lines": [
{
"title": "standard",
"price": 0.00,
"code": null,
"source": "brand owner on shopify",
"carrier_identifier": null,
"tax_lines": null
}
],
"tags": "someName"
}
}
{
"variant": {
"id": 21718275463,
"product_id": 6820646151,
"title": "m / red",
"price": "99.00",
"sku": "",
"position": 2,
"grams": 0,
"inventory_policy": "deny",
"compare_at_price": "900.00",
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "m",
"option2": "red",
"option3": null,
"created_at": "2016-05-27T13:16:26-04:00",
"updated_at": "2016-05-28T13:28:20-04:00",
"taxable": false,
"barcode": "",
"image_id": 13217378823,
"inventory_quantity": 1,
"weight": 0,
"weight_unit": "lb",
"old_inventory_quantity": 1,
"requires_shipping": true
}
}
{
"variant": {
"id": 21717700871,
"product_id": 6820646151,
"title": "s / green",
"price": "1000.00",
"sku": "",
"position": 1,
"grams": 0,
"inventory_policy": "deny",
"compare_at_price": "1111.00",
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "s",
"option2": "green",
"option3": null,
"created_at": "2016-05-27T13:05:56-04:00",
"updated_at": "2016-05-28T12:17:22-04:00",
"taxable": true,
"barcode": "",
"image_id": 13160712135,
"inventory_quantity": 2,
"weight": 0,
"weight_unit": "lb",
"old_inventory_quantity": 2,
"requires_shipping": true
}
}
{
"variant": {
"id": 21717690055,
"product_id": 6821668807,
"title": "Default Title",
"price": "555.00",
"sku": "",
"position": 1,
"grams": 0,
"inventory_policy": "deny",
"compare_at_price": "666.00",
"fulfillment_service": "manual",
"inventory_management": "shopify",
"option1": "Default Title",
"option2": null,
"option3": null,
"created_at": "2016-05-27T13:05:39-04:00",
"updated_at": "2016-05-28T12:17:22-04:00",
"taxable": true,
"barcode": "",
"image_id": null,
"inventory_quantity": 2,
"weight": 0,
"weight_unit": "lb",
"old_inventory_quantity": 2,
"requires_shipping": true
}
}
You created a fake order using the API. Fake orders like that don't transact money or trigger the usual internal checks and balances without some extra effort. Maybe if you tried adding a fulfillment to the order, Shopify might ding inventory levels? Seems like something to try anyway.
I got a JSON object that I have generated via Rails 3 (via my API). I need to either put a named surrounding tag around it or loose the first "layer". I know it sounds very strange, but I cannot loop it in the client.
This is the parsing code:
#results = RestClient.get "http://localhost:5000/emails?token=#{get_token}", :accept => :json
#array = JSON.parse(#results);
Turn this:
[
{
"email": {
"created_at": "2011-03-02T12:23:59Z",
"updated_at": "2011-03-02T12:23:59Z",
"value_1": "intevisa#sss.com",
"value_2": null,
"id": 4,
"value_3": null,
"user_id": 1,
"value_4": null,
"desc": "intevisa",
"privacy": null
}
},
{
"email": {
"created_at": "2011-03-02T15:19:39Z",
"updated_at": "2011-03-02T15:19:39Z",
"value_1": "another#yahoo.com",
"value_2": null,
"id": 5,
"value_3": null,
"user_id": 1,
"value_4": null,
"desc": "Some text",
"privacy": null
}
},
{
"email": {
"created_at": "2011-03-02T15:20:17Z",
"updated_at": "2011-03-02T15:20:17Z",
"value_1": "my#email.com",
"value_2": null,
"id": 6,
"value_3": null,
"user_id": 1,
"value_4": null,
"desc": "Some text",
"privacy": null
}
},
{
"email": {
"created_at": "2011-03-02T15:21:03Z",
"updated_at": "2011-03-02T15:21:03Z",
"value_1": "An email#google.com",
"value_2": null,
"id": 7,
"value_3": null,
"user_id": 1,
"value_4": null,
"desc": "Hello world",
"privacy": null
}
}
]
Into this:
[
"email": {
"created_at": "2011-03-02T12:23:59Z",
"updated_at": "2011-03-02T12:23:59Z",
"value_1": "intevisa#sss.com",
"value_2": null,
"id": 4,
"value_3": null,
"user_id": 1,
"value_4": null,
"desc": "intevisa",
"privacy": null
},
"email": {
"created_at": "2011-03-02T15:19:39Z",
"updated_at": "2011-03-02T15:19:39Z",
"value_1": "another#yahoo.com",
"value_2": null,
"id": 5,
"value_3": null,
"user_id": 1,
"value_4": null,
"desc": "Some text",
"privacy": null
},
"email": {
"created_at": "2011-03-02T15:20:17Z",
"updated_at": "2011-03-02T15:20:17Z",
"value_1": "my#email.com",
"value_2": null,
"id": 6,
"value_3": null,
"user_id": 1,
"value_4": null,
"desc": "Some text",
"privacy": null
},
"email": {
"created_at": "2011-03-02T15:21:03Z",
"updated_at": "2011-03-02T15:21:03Z",
"value_1": "An email#google.com",
"value_2": null,
"id": 7,
"value_3": null,
"user_id": 1,
"value_4": null,
"desc": "Hello world",
"privacy": null
}
]
This is how I try to loop through it in the client.
<% #array['email'].each do |item| %>
<%= item['value_1'] %>
<% end %>
Try the following in config > initializers > wrap_parameters.rb
# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
ActionController::Base.wrap_parameters format: [:json]
# Disable root element in JSON by default.
if defined?(ActiveRecord)
ActiveRecord::Base.include_root_in_json = false
end
By setting the "include_root_in_json" to false, it should get you the desired output