How to upsert new record in Prisma without an ID?How do I debug Node.js applications?How do I get started with Node.jsHow do I pass command line arguments to a Node.js program?How to decide when to use Node.js?How to exit in Node.jsWhat is the purpose of Node.js module.exports and how do you use it?How can I update NodeJS and NPM to the next versions?How do I update each dependency in package.json to the latest version?npm throws error without sudoPrisma returning error of expected input type but type is provided
If a spaceship ran out of fuel somewhere in space between Earth and Mars, does it slowly drift off to the Sun?
Is it ok if I haven't decided my research topic when I first meet with a potential phd advisor?
"until mine is on tight" is a idiom?
What is the white pattern on trim wheel for?
Youtube not blocked by iptables
Does the app TikTok violate trademark?
Clear text passwords in Unix
What exactly did this mechanic sabotage on the American Airlines 737, and how dangerous was it?
Why does (inf + 0j)*1 evaluate to inf + nanj?
Would you write key signatures for non-conventional scales?
What does it mean by "my days-of-the-week underwear only go to Thursday" in this context?
Why does my browser attempt to download pages from http://clhs.lisp.se instead of viewing them normally?
How to realistically describe pain?
My manager quit. Should I agree to defer wage increase to accommodate budget concerns?
Is there a concept of "peer review" in Rabbinical Judaism?
Is the order of words purely based on convention?
I cannot take my Macbook Pro 2015 in my Aegean/Lufthansa flight?
Difference between "rip up" and "rip down"
Two side-by-side squares are inscribed in a semicircle. The diameter of the semicircle is 16. What is the sum of the two squares' areas?
What secular civic space would pioneers build for small frontier towns?
Received a package but didn't order it
I reverse the source code, you reverse the input!
Do interval ratios take overtones into account or solely the fundamental frequency?
I am not a pleasant sight
How to upsert new record in Prisma without an ID?
How do I debug Node.js applications?How do I get started with Node.jsHow do I pass command line arguments to a Node.js program?How to decide when to use Node.js?How to exit in Node.jsWhat is the purpose of Node.js module.exports and how do you use it?How can I update NodeJS and NPM to the next versions?How do I update each dependency in package.json to the latest version?npm throws error without sudoPrisma returning error of expected input type but type is provided
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm using Prisma (https://www.prisma.io) as ORM. I want to check for duplicates when store data and, if not exists, create a new record.
I thought I could do that with upsert method provided by Prisma and available in the generated client, but the where clause of that method only works with id (or @unique fields), but if the record doesn't exist, there isn't any id to provide.
I provide an example of the problem.
datamodel.prisma
type System
id: ID! @unique
performances: [SystemPerformance!]! @relation(name: "PerformanceBySystem" onDelete: CASCADE)
name: String! @unique
type SystemPerformance
id: ID! @unique
system: System! @relation(name: "PerformanceBySystem")
date: DateTime!
perf1: Float
perf2: Float
seed.js
const prisma = require('./generated/prisma-client');
async function main()
await prisma.createSystem(
name: 's1',
);
await prisma.createSystem(
name: 's2',
);
await prisma.createSystem(
name: 's3',
);
main();
After creation there is a database with three Systems without performances. I'm trying to insert a new SystemPerformance if there aren't any that have same date and same System. I have tried
const prisma = require('./prisma/generated/prisma-client');
const perf = await prisma.upsertSystemPerformance(
where:
system: name: 's1',
date: "2019-03-12T00:01:06.000Z"
,
update:
perf1: 13.45,
perf2: 18.93
,
create:
system:
connect: name: 's1'
,
date: "2019-03-12T00:01:06.000Z",
perf1: 13.45,
perf2: 18.93
)
But an exception is thrown:
UnhandledPromiseRejectionWarning: Error: Variable '$where' expected value of type 'SystemPerformanceWhereUniqueInput!' but got: "system":"name":'s1',"date":"2019-03-12T00:01:06.000Z". Reason: 'system' Field 'system' is not defined in the input type 'SystemPerformanceWhereUniqueInput'
The only solution I have found is check for existence and then update or create, but I wanted to do it with upsert.
let check = await prisma.$exists.SystemPerformance(
system: name: 's1',
date: "2019-03-12T00:01:06.000Z"
);
let perfo;
if (check)
const sysPerf = await prisma.systemPerformances(where:system: name: 's1', date: "2019-03-12T00:01:06.000Z")
.$fragment(`
id
`);
perfo = await prisma.updateSystemPerformance(
where: id: sysPerf[0].id,
data:
perf1: 13.45,
perf2: 18.93
)
else
perfo = await prisma.createSystemPerformance(
system:
connect: name: 's1'
,
date: "2019-03-12T00:01:06.000Z",
perf1: 13.45,
perf2: 18.93
)
Is there a way to do that with upsert?
node.js prisma prisma-graphql
add a comment
|
I'm using Prisma (https://www.prisma.io) as ORM. I want to check for duplicates when store data and, if not exists, create a new record.
I thought I could do that with upsert method provided by Prisma and available in the generated client, but the where clause of that method only works with id (or @unique fields), but if the record doesn't exist, there isn't any id to provide.
I provide an example of the problem.
datamodel.prisma
type System
id: ID! @unique
performances: [SystemPerformance!]! @relation(name: "PerformanceBySystem" onDelete: CASCADE)
name: String! @unique
type SystemPerformance
id: ID! @unique
system: System! @relation(name: "PerformanceBySystem")
date: DateTime!
perf1: Float
perf2: Float
seed.js
const prisma = require('./generated/prisma-client');
async function main()
await prisma.createSystem(
name: 's1',
);
await prisma.createSystem(
name: 's2',
);
await prisma.createSystem(
name: 's3',
);
main();
After creation there is a database with three Systems without performances. I'm trying to insert a new SystemPerformance if there aren't any that have same date and same System. I have tried
const prisma = require('./prisma/generated/prisma-client');
const perf = await prisma.upsertSystemPerformance(
where:
system: name: 's1',
date: "2019-03-12T00:01:06.000Z"
,
update:
perf1: 13.45,
perf2: 18.93
,
create:
system:
connect: name: 's1'
,
date: "2019-03-12T00:01:06.000Z",
perf1: 13.45,
perf2: 18.93
)
But an exception is thrown:
UnhandledPromiseRejectionWarning: Error: Variable '$where' expected value of type 'SystemPerformanceWhereUniqueInput!' but got: "system":"name":'s1',"date":"2019-03-12T00:01:06.000Z". Reason: 'system' Field 'system' is not defined in the input type 'SystemPerformanceWhereUniqueInput'
The only solution I have found is check for existence and then update or create, but I wanted to do it with upsert.
let check = await prisma.$exists.SystemPerformance(
system: name: 's1',
date: "2019-03-12T00:01:06.000Z"
);
let perfo;
if (check)
const sysPerf = await prisma.systemPerformances(where:system: name: 's1', date: "2019-03-12T00:01:06.000Z")
.$fragment(`
id
`);
perfo = await prisma.updateSystemPerformance(
where: id: sysPerf[0].id,
data:
perf1: 13.45,
perf2: 18.93
)
else
perfo = await prisma.createSystemPerformance(
system:
connect: name: 's1'
,
date: "2019-03-12T00:01:06.000Z",
perf1: 13.45,
perf2: 18.93
)
Is there a way to do that with upsert?
node.js prisma prisma-graphql
Upsert accepts only unique fields as input inwhere
clause. I fear you will have to go with workaround where you query record and then use upsert or update / create depending upon whether record exists.
– User97
Apr 1 at 5:31
I was afraid of that. I asked if there was a better solution. Thank you for your answer.
– Jose Ángel de Pascual Viciana
Apr 3 at 13:18
add a comment
|
I'm using Prisma (https://www.prisma.io) as ORM. I want to check for duplicates when store data and, if not exists, create a new record.
I thought I could do that with upsert method provided by Prisma and available in the generated client, but the where clause of that method only works with id (or @unique fields), but if the record doesn't exist, there isn't any id to provide.
I provide an example of the problem.
datamodel.prisma
type System
id: ID! @unique
performances: [SystemPerformance!]! @relation(name: "PerformanceBySystem" onDelete: CASCADE)
name: String! @unique
type SystemPerformance
id: ID! @unique
system: System! @relation(name: "PerformanceBySystem")
date: DateTime!
perf1: Float
perf2: Float
seed.js
const prisma = require('./generated/prisma-client');
async function main()
await prisma.createSystem(
name: 's1',
);
await prisma.createSystem(
name: 's2',
);
await prisma.createSystem(
name: 's3',
);
main();
After creation there is a database with three Systems without performances. I'm trying to insert a new SystemPerformance if there aren't any that have same date and same System. I have tried
const prisma = require('./prisma/generated/prisma-client');
const perf = await prisma.upsertSystemPerformance(
where:
system: name: 's1',
date: "2019-03-12T00:01:06.000Z"
,
update:
perf1: 13.45,
perf2: 18.93
,
create:
system:
connect: name: 's1'
,
date: "2019-03-12T00:01:06.000Z",
perf1: 13.45,
perf2: 18.93
)
But an exception is thrown:
UnhandledPromiseRejectionWarning: Error: Variable '$where' expected value of type 'SystemPerformanceWhereUniqueInput!' but got: "system":"name":'s1',"date":"2019-03-12T00:01:06.000Z". Reason: 'system' Field 'system' is not defined in the input type 'SystemPerformanceWhereUniqueInput'
The only solution I have found is check for existence and then update or create, but I wanted to do it with upsert.
let check = await prisma.$exists.SystemPerformance(
system: name: 's1',
date: "2019-03-12T00:01:06.000Z"
);
let perfo;
if (check)
const sysPerf = await prisma.systemPerformances(where:system: name: 's1', date: "2019-03-12T00:01:06.000Z")
.$fragment(`
id
`);
perfo = await prisma.updateSystemPerformance(
where: id: sysPerf[0].id,
data:
perf1: 13.45,
perf2: 18.93
)
else
perfo = await prisma.createSystemPerformance(
system:
connect: name: 's1'
,
date: "2019-03-12T00:01:06.000Z",
perf1: 13.45,
perf2: 18.93
)
Is there a way to do that with upsert?
node.js prisma prisma-graphql
I'm using Prisma (https://www.prisma.io) as ORM. I want to check for duplicates when store data and, if not exists, create a new record.
I thought I could do that with upsert method provided by Prisma and available in the generated client, but the where clause of that method only works with id (or @unique fields), but if the record doesn't exist, there isn't any id to provide.
I provide an example of the problem.
datamodel.prisma
type System
id: ID! @unique
performances: [SystemPerformance!]! @relation(name: "PerformanceBySystem" onDelete: CASCADE)
name: String! @unique
type SystemPerformance
id: ID! @unique
system: System! @relation(name: "PerformanceBySystem")
date: DateTime!
perf1: Float
perf2: Float
seed.js
const prisma = require('./generated/prisma-client');
async function main()
await prisma.createSystem(
name: 's1',
);
await prisma.createSystem(
name: 's2',
);
await prisma.createSystem(
name: 's3',
);
main();
After creation there is a database with three Systems without performances. I'm trying to insert a new SystemPerformance if there aren't any that have same date and same System. I have tried
const prisma = require('./prisma/generated/prisma-client');
const perf = await prisma.upsertSystemPerformance(
where:
system: name: 's1',
date: "2019-03-12T00:01:06.000Z"
,
update:
perf1: 13.45,
perf2: 18.93
,
create:
system:
connect: name: 's1'
,
date: "2019-03-12T00:01:06.000Z",
perf1: 13.45,
perf2: 18.93
)
But an exception is thrown:
UnhandledPromiseRejectionWarning: Error: Variable '$where' expected value of type 'SystemPerformanceWhereUniqueInput!' but got: "system":"name":'s1',"date":"2019-03-12T00:01:06.000Z". Reason: 'system' Field 'system' is not defined in the input type 'SystemPerformanceWhereUniqueInput'
The only solution I have found is check for existence and then update or create, but I wanted to do it with upsert.
let check = await prisma.$exists.SystemPerformance(
system: name: 's1',
date: "2019-03-12T00:01:06.000Z"
);
let perfo;
if (check)
const sysPerf = await prisma.systemPerformances(where:system: name: 's1', date: "2019-03-12T00:01:06.000Z")
.$fragment(`
id
`);
perfo = await prisma.updateSystemPerformance(
where: id: sysPerf[0].id,
data:
perf1: 13.45,
perf2: 18.93
)
else
perfo = await prisma.createSystemPerformance(
system:
connect: name: 's1'
,
date: "2019-03-12T00:01:06.000Z",
perf1: 13.45,
perf2: 18.93
)
Is there a way to do that with upsert?
node.js prisma prisma-graphql
node.js prisma prisma-graphql
asked Mar 28 at 18:35
Jose Ángel de Pascual VicianaJose Ángel de Pascual Viciana
134 bronze badges
134 bronze badges
Upsert accepts only unique fields as input inwhere
clause. I fear you will have to go with workaround where you query record and then use upsert or update / create depending upon whether record exists.
– User97
Apr 1 at 5:31
I was afraid of that. I asked if there was a better solution. Thank you for your answer.
– Jose Ángel de Pascual Viciana
Apr 3 at 13:18
add a comment
|
Upsert accepts only unique fields as input inwhere
clause. I fear you will have to go with workaround where you query record and then use upsert or update / create depending upon whether record exists.
– User97
Apr 1 at 5:31
I was afraid of that. I asked if there was a better solution. Thank you for your answer.
– Jose Ángel de Pascual Viciana
Apr 3 at 13:18
Upsert accepts only unique fields as input in
where
clause. I fear you will have to go with workaround where you query record and then use upsert or update / create depending upon whether record exists.– User97
Apr 1 at 5:31
Upsert accepts only unique fields as input in
where
clause. I fear you will have to go with workaround where you query record and then use upsert or update / create depending upon whether record exists.– User97
Apr 1 at 5:31
I was afraid of that. I asked if there was a better solution. Thank you for your answer.
– Jose Ángel de Pascual Viciana
Apr 3 at 13:18
I was afraid of that. I asked if there was a better solution. Thank you for your answer.
– Jose Ángel de Pascual Viciana
Apr 3 at 13:18
add a comment
|
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55404678%2fhow-to-upsert-new-record-in-prisma-without-an-id%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55404678%2fhow-to-upsert-new-record-in-prisma-without-an-id%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Upsert accepts only unique fields as input in
where
clause. I fear you will have to go with workaround where you query record and then use upsert or update / create depending upon whether record exists.– User97
Apr 1 at 5:31
I was afraid of that. I asked if there was a better solution. Thank you for your answer.
– Jose Ángel de Pascual Viciana
Apr 3 at 13:18