I have two tables called user and post which they have data like this:
Users Table
id | first_name | second_name
1 John Doe
2 Ellis Mount
Posts Table
id | topic | body | author_id
1 Internet ...some text 1
2 Web 3.0 ...some text 2
So what my question is how to how to query posts with the author user inside of it as object referring to the user who created the post.
[
{
id:1,
topic: Internet,
body: ...some text,
author_id: 1
author: {
first_name: John,
last_name: Doe
}
},
{
id: 2,
topic: Web 3.0,
body: ...some text,
author_id: 2
author: {
first_name: Ellis,
last_name: Mount
}
}
]
So, in a way to reach this, I tried something like this with `inner join:
SELECT *
FROM post
INNER JOIN user
ON post.author_id = user.id;
unfortunately, this not fetching the data I want... so if any know how to query this please help!
You can use JSON format and create JSON filed.
Demo
SELECT
post.*,
json_build_object(
'first_name',
first_name,
'last_name',
second_name
) as author
FROM post
INNER JOIN "user"
ON post.author_id = "user".id;
Related
Hello i am making recipe app and at this point i have to create editing functionality by getting update mutation from hasura.But i have issues trying to create this mutation.Because there are two tables related to each other, one of them is recipes and other related by id is ingredient which is a array of objects .I need to populate existing form with a dynamic ingredient field with existing recipe data and then to be able to edit that data .At first i thought i will be able to create something similar to what i did with insert mutation but inserting and updating have diferent properties and im abit lost here.
here is how i wrote my insertion mutation which is working fine
mutation insertRecipe(
$title: String!
$image: String!
$description: String!
$Date: date!
$ingredient_relation: [ingredient_insert_input!]!
) {
insert_recipes(
objects: {
title: $title
image: $image
description: $description
Date: $Date
ingredient_relation: { data: $ingredient_relation }
}
) {
returning {
id
}
}
}
and here is my atempt at updating mutation but update doesent have data property which i used in insert mutation
mutation recipe_edit(
$title: String!
$id: Int!
$image: String!
$description: String!
$Date: date!
$ingredient_relation: [ingredient_insert_input!]!
) {
update_recipes(
_set: {
title: $title
image: $image
description: $description
Date: $Date
}
where: { id: { _eq: $id } }
) {
returning {
id
}
}
update_ingredient(
_set: { data: $ingredient_relation }
where: { recipe_id: { _eq: $id } }
) {
returning {
id
}
}
}
I also made fully working updating withouth variables it works only in hasura graphiql interface
mutation UpdateRecipe {
update_recipes(_set: {title: "lets change title", image: "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5e/Vytautas_the_Great_Bridge_from_hill%2C_Kaunas%2C_Lithuania_-_Diliff.jpg/1280px-Vytautas_the_Great_Bridge_from_hill%2C_Kaunas%2C_Lithuania_-_Diliff.jpgs", description: "new description", Date: "1991-06-09"}, where: {id: {_eq: 10}}) {
affected_rows
}
update_ingredient(_set: {name: "lets change the name"}, where: {recipe_id: {_eq: 10}}) {
affected_rows
}
}
Unfortunately, you cannot update multiple records by ID in 1 mutation section.
There are 2 approaches I typically use for this issue.
1: use an insert mutation with an on_conflict constraint for the table ID and supply all columns you wish to update if they conflict to the update_columns parameter. You just declare a single variable of the [table]_insert_input type and pass in the updated record as a JS object (instead of passing in each value separately). You can nest child records here as well. You will need to declare conflict constraints and columns to update for the children as well. Messing around with the graphiql interface should help you figure out how to do this. This has the benefit of using the same query for both insert and update operations. It has the same downside as well... (permissions)
2: using code (presumably) you can generate as many update graphql sections as you'd like. Like so:
const MUTATION = `mutation UpdateRecipe {
update_recipes(_set: {title: "lets change title", image: "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5e/Vytautas_the_Great_Bridge_from_hill%2C_Kaunas%2C_Lithuania_-_Diliff.jpg/1280px-Vytautas_the_Great_Bridge_from_hill%2C_Kaunas%2C_Lithuania_-_Diliff.jpgs", description: "new description", Date: "1991-06-09"}, where: {id: {_eq: 10}}) {
affected_rows
}
${
ingredients.map((ingrd, i) => `
update_ingredient_${i}: update_ingredient(_set: {name: ${ingrd.name}}, where: {id: {_eq: ${ingrd.id}}}) {
returning { ... }
}
`).join("\n")
}
}`
Notice I prefixed each section because 1 query cannot have multiple sections of the same name.
While option 2 certainly feels "icky"er it has the single benefit of using the correct permission for the operation...
Hope this helps
I'm trying to do the following using Prisma:
If a category row with the same hash and user_id I have exists, just update it's "name" field, otherwise, create the row
Is this possible? TS is giving me an error saying that the type of the key given on "where" has to be of categoriesWhereUniqueInput, yet neither hash nor user_id are unique, they can repeat, it's the combination between the two that's gonna be unique
How can I work around this? Do I have to manually check if there's an id and update/create based on that?
Thanks a lot in advance!
const category = await prisma.categories.upsert({
where: {
hash,
user_id: id,
},
update: {
name,
},
create: {
hash,
name,
user_id: id,
},
});
When a combination of fields is unique, Prisma will generate a new type for the where condition which is basically just the name of all the relevant fields appended together with _.
Take a look at the categoriesWhereUniqueInput to see what the name is. Most likely it is called user_id_hash or hash_user_id.
This is what the query will look like, roughly speaking:
const category = await prisma.categories.upsert({
where: {
user_id_hash: { // user_id_hash is the type generated by Prisma. Might be called something else though.
user_id: 1,
hash: "foo"
}
},
update: {
name: "bar",
},
create: {
hash: "foo",
name: "bar",
user_id: 1,
},
})
I have a DB table which stores id, cart(array), updated_at, created_at. How can I deconstruct the array and display the items in the view as list items.
3
{"3":{"name":"iPhone","quantity":2,"price":"500.00"},"4":
{"name":"Samsung","quantity":1,"price":"344.00"}}
2019-11-10 07:50:53
______________________
ProductsController
______________________
public function cartsIndex(Request $request)
{
$carts = Cart::all();
$carts = json_decode($carts);
// return view('cartsIndex')->with(['carts' => $carts]);
dd($carts);
}
I want the cart to be displayed as below.
ID - 3
Name: Iphone
Quantity: 2
Price: 500
Name: Samsung
Quantity: 1
Price: 344
use laravel's $cast attribute in your model. It will automatically help you to convert your data to json while storing in database and will help you access them as array when you retrieve them.
protected $cast = [
'cart' => 'array' // name of the column you want to store as json and retrieve as array
];
for reference https://laravel.com/docs/6.x/eloquent-mutators#array-and-json-casting
I have a schema as follows:
type User #model {
id: ID!
firstName: String!
lastName: String!
locations: [Location]
}
type Location #model {
id: ID!
listOfPictures: [String]
address: String!
owner: User
}
Currently I have entries to both Locations and Users. The entries for my Locations are all null.
I want to update one of my Locations, of ID '1234' to have the field 'owner' set to the User of ID '5678'.
What is the correct syntax for this? What do I have to enter into he Appsync console?
Additionally, how could I update the 'locations' field of my User of id 'abc' model to add a Location, to the array of Locations, of Location with id 'def' ?
Thanks.
You can update your data by using mutations: https://docs.aws.amazon.com/appsync/latest/devguide/quickstart-write-queries.html
I'm using urigo:angular package. In my project I have two collections: posts and users. Is it possible to publish from server an object, which looks like this:
[{createdAt: 20-12-2015,
text: 'something',
user: { name: 'some name'}
},
{createdAt: 22-12-2015,
text: 'something2',
user: { name: 'some other name'}
}]
I mean insert user object to every post.
Update:
I have two collectionsL posts and users. Every post has user id. When I do something like this with publishComposite: https://github.com/Nitrooos/Forum-Steganum/blob/posts/server/posts.methods.coffee then I have on the client side (https://github.com/Nitrooos/Forum-Steganum/blob/posts/client/posts/posts.controller.coffee) only an array with posts, without the user in it.
I have this:
[{createdAt: 20-12-2015,
text: 'something',
userId: 123
},
{createdAt: 22-12-2015,
text: 'something2',
userId: 123
}]
So, when I'll want display a username, I'll have to do a request to every post about user?