prisma1 icon indicating copy to clipboard operation
prisma1 copied to clipboard

Create mutation attempts to create unwanted nested data

Open br-dev opened this issue 7 years ago • 2 comments

Describe the bug

I have an issue where I try to create and connect an object (Company) to its associated user, but the actual insert in the DB function attempts to insert a blank Store which is owned by a Company.

My datamodel.prisma looks like this:

type Account @pgTable(name: "account") {
  id: Int! @id @unique
  email: String! @unique
  username: String! @unique
  stores: [Store!]!
  companies: [Company!]!
}

type Company @pgTable(name: "company") {
  id: Int! @id @unique
  name: String
  stores: [Store!]!
  user: Account @pgRelation(column: "userId")
}

type Store @pgTable(name: "store") {
  id: Int! @id @unique
  storeId: String!
  company: Company @pgRelation(column: "companyId")
  user: Account @pgRelation(column: "userId")
}

Each user can have multiple companies, and each company can have multiple stores. To simplify querying, I also associate the stores with the user.

This datamodel was created with prisma intropect.

The issue is when I try to create a new company (user.sub is the user's ID):

createCompany: (parent, args, ctx: Context) => {
  const user = getUser(ctx);
  return ctx.prisma.createCompany({
    ...args.company, // Includes 'name'
    user: { connect: { id: user.sub } }, // Error occurs when this is added
  });
},

If I try to associate the user's ID with the company, then the createCompany try's to also create an empty Store. This fails, because I have contraints on my DB which disallow a blank store (name). If however, I leave the connect property out, then the Company is created with no issues (except it is not associated to a user - not wanted).

Here is the error I recieve in my Docker contaier:

{"key":"error/unhandled","requestId":"local:cjrk2gfs400440747axvo9c1v","clientId":"default$default","payload":{"exception":"org.postgresql.util.PSQLException: ERROR: null value in column \"storeId\" violates not-null constraint\n  Detail: Failing row contains (8, null, null, null, null, null, null, null, t, null, null, null, null, null, 10, 1).","query":"mutation ($data: CompanyCreateInput!) {\n  createCompany(data: $data) {\n    id\n  name\n      }\n}\n","variables":"{\"data\":{\"name\":\"Company1\",\"user\":{\"connect\":{\"id\":1}}}}","code":"0","stack_trace":"org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433)\\n org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2178)\\n org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:306)\\n org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)\\n org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)\\n org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:155)\\n org.postgresql.jdbc.PgPreparedStatement.execute(PgPreparedStatement.java:144)\\n com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44)\\n com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute(HikariProxyPreparedStatement.java)\\n com.prisma.connector.shared.jdbc.SharedSlickExtensions.$anonfun$insertToDBIO$1(SharedSlickExtensions.scala:43)\\n com.prisma.connector.shared.jdbc.SharedSlickExtensions.$anonfun$insertToDBIO$1$adapted(SharedSlickExtensions.scala:43)\\n com.prisma.connector.shared.jdbc.SharedSlickExtensions.$anonfun$jooqToDBIO$1(SharedSlickExtensions.scala:62)\\n slick.jdbc.SimpleJdbcAction.run(StreamingInvokerAction.scala:70)\\n slick.jdbc.SimpleJdbcAction.run(StreamingInvokerAction.scala:69)\\n slick.basic.BasicBackend$DatabaseDef$$anon$2.liftedTree1$1(BasicBackend.scala:275)\\n slick.basic.BasicBackend$DatabaseDef$$anon$2.run(BasicBackend.scala:275)\\n java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)\\n java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)\\n java.lang.Thread.run(Thread.java:748)","message":"ERROR: null value in column \"storeId\" violates not-null constraint\n  Detail: Failing row contains (8, null, null, null, null, null, null, null, t, null, null, null, null, null, 10, 1)."}}
[Bugsnag - local / testing] Error report: com.bugsnag.Report@341ab027
at com.prisma.connector.shared.jdbc.SharedSlickExtensions.$anonfun$insertToDBIO$1$adapted(SharedSlickExtensions.scala:43)
at com.prisma.connector.shared.jdbc.SharedSlickExtensions.$anonfun$jooqToDBIO$1(SharedSlickExtensions.scala:62)
at slick.jdbc.SimpleJdbcAction.run(StreamingInvokerAction.scala:70)
at slick.jdbc.SimpleJdbcAction.run(StreamingInvokerAction.scala:69)
at slick.basic.BasicBackend$DatabaseDef$$anon$2.liftedTree1$1(BasicBackend.scala:275)
at slick.basic.BasicBackend$DatabaseDef$$anon$2.run(BasicBackend.scala:275)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
org.postgresql.util.PSQLException: ERROR: null value in column "storeId" violates not-null constraint
  Detail: Failing row contains (8, null, null, null, null, null, null, null, t, null, null, null, null, null, 10, 1).
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2178)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:306)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:155)
at org.postgresql.jdbc.PgPreparedStatement.execute(PgPreparedStatement.java:144)
at com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44)
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute(HikariProxyPreparedStatement.java)
at com.prisma.connector.shared.jdbc.SharedSlickExtensions.$anonfun$insertToDBIO$1(SharedSlickExtensions.scala:43)
at com.prisma.connector.shared.jdbc.SharedSlickExtensions.$anonfun$insertToDBIO$1$adapted(SharedSlickExtensions.scala:43)
at com.prisma.connector.shared.jdbc.SharedSlickExtensions.$anonfun$jooqToDBIO$1(SharedSlickExtensions.scala:62)
at slick.jdbc.SimpleJdbcAction.run(StreamingInvokerAction.scala:70)
at slick.jdbc.SimpleJdbcAction.run(StreamingInvokerAction.scala:69)
at slick.basic.BasicBackend$DatabaseDef$$anon$2.liftedTree1$1(BasicBackend.scala:275)
at slick.basic.BasicBackend$DatabaseDef$$anon$2.run(BasicBackend.scala:275)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

To Reproduce

  1. Create Postgres Tables:

Account/User:

CREATE TABLE public.account
(
    id integer NOT NULL DEFAULT nextval('users_id_seq'::regclass),
    username character varying(255) COLLATE pg_catalog."default" NOT NULL,
    email character varying(255) COLLATE pg_catalog."default" NOT NULL,
    CONSTRAINT users_pkey PRIMARY KEY (id),
    CONSTRAINT users_username_email_unique UNIQUE (username, email)

)

Company:

CREATE TABLE public.company
(
    id integer NOT NULL DEFAULT nextval('companies_id_seq'::regclass),
    name character varying(255) COLLATE pg_catalog."default",
    "userId" integer,
    CONSTRAINT companies_pkey PRIMARY KEY (id),
,
    CONSTRAINT companies_userid_foreign FOREIGN KEY ("userId")
        REFERENCES public.account (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE CASCADE
)

Store:

CREATE TABLE public.store
(
    id integer NOT NULL DEFAULT nextval('stores_id_seq'::regclass),
    "storeId" character varying(255) COLLATE pg_catalog."default" NOT NULL,
    "companyId" integer,
    "userId" integer,
    CONSTRAINT stores_pkey PRIMARY KEY (id),
    CONSTRAINT store_userid_foreign FOREIGN KEY ("userId")
        REFERENCES public.account (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT stores_companyid_foreign FOREIGN KEY ("companyId")
        REFERENCES public.company (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE CASCADE
)
  1. Run prisma introspect to create your datamodel, deploy and generate your datamodel
  2. Create a user, and use the mutation above to attempt to create a company and associate it with the user

Expected behavior

I should be able to create a company without also having to create a store in the database

Screenshots

None

Versions (please complete the following information):

  • Connector:Postgres
  • prisma/1.25.3 (darwin-x64) node-v10.13.0
  • OS: OS X Mojave

Additional context

Add any other context about the problem here.

br-dev avatar Jan 31 '19 03:01 br-dev

I was able to reproduce this issue. The above SQL has some missing sequences so here is a proper reproduction: https://github.com/pantharshit00/prisma-issue-3960

pantharshit00 avatar Feb 02 '19 17:02 pantharshit00

This is indeed a bug but there's no easy way to fix it right now. However i tested this with our upcoming Datamodel v1.1 and there the bug does not occur. Therefore i suggest upgrading to the new Datamodel once we released it.

mavilein avatar Feb 18 '19 10:02 mavilein