Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Netmask is absent when selecting INET datatype #17929

Open
RrredHead opened this issue Feb 15, 2023 Discussed in #17903 · 3 comments
Open

Netmask is absent when selecting INET datatype #17929

RrredHead opened this issue Feb 15, 2023 Discussed in #17903 · 3 comments
Labels
bug/2-confirmed Bug has been reproduced and confirmed. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/bug A reported bug. topic: broken query topic: postgresql

Comments

@RrredHead
Copy link

RrredHead commented Feb 15, 2023

Discussed in #17903

I have a project with table configured like that

generator client {
    provider        = "prisma-client-js"
    previewFeatures = ["metrics"]
}

datasource db {
    provider = "postgresql"
    url      = env("DATABASE_URL")
}

// ...

model IP {
    id          Int      @id @default(autoincrement())
    address     String   @unique @db.Inet
    description String   @default("")
    updatedAt   DateTime @updatedAt
}

Which gives me table as I expected (DDL as example):

CREATE TABLE public."IP" (
	id serial4 NOT NULL,
	description text NOT NULL DEFAULT ''::text,
	"updatedAt" timestamp(3) NOT NULL,
	address inet NOT NULL,
	CONSTRAINT "IP_pkey" PRIMARY KEY (id)
);
CREATE UNIQUE INDEX "IP_address_key" ON public."IP" USING btree (address);

In this table I have only one record (for test purposes):

image

Also I have a route, which should just return every records stored in IP table

const ips = await prisma.iP.findMany();
return ips;

As a result it gives:

[
  {
    "id": 2,
    "description": "Localhost",
    "updatedAt": "2023-02-13T14:05:04.121Z",
    "address": "127.0.0.0"
  }
]

Everything is nearly correct, but there is no mask in address field. In this case I'm expecting "address": "127.0.0.0/16" as it stored in Data Base.

The netmask should be in the output if it is not the single host, as it said in the reference here.

If the netmask is 32 and the address is IPv4, then the value does not indicate a subnet, only a single host.

and

On display, the /y portion is suppressed if the netmask specifies a single host.

I've found one workaround:

async function findIPs() {
    const result = await prisma.$queryRaw<
        IP[]
    >`SELECT id, description, "updatedAt", text(address) as address FROM public."IP"`;
    return result;
}

// ... later in route handler

const ips = await findIPs();
return ips;

This gives me output as I expected:

[
  {
    "id": 2,
    "description": "Localhost",
    "updatedAt": "2023-02-13T14:05:04.121Z",
    "address": "127.0.0.0/16"
  }
]

So everything is looks like function host applied somewhere under the hood of prisma (or maybe even pg driver) (take look at Table 9-32 here in reference). Or some other same thing happens. But I'm quite sure that function text have to be applied when casting postgres INET datatype to JS String by default (and I found no proves that it is actually done).

@mcbethio mcbethio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. topic: postgresql topic: broken query labels Feb 15, 2023
@mcbethio
Copy link

I can confirm, very simple to reproduce. Thanks for the bug report @RrredHead

@janpio janpio added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Feb 17, 2023
@RrredHead
Copy link
Author

RrredHead commented Feb 20, 2023

Please check also inserting into table which containing inet column. I'm facing this error:

debug: prisma:query {"duration":4,"params":"[]","query":"SELECT id, description, \"updatedAt\", text(address) as address FROM \"IP\""}
debug: prisma:query {"duration":4,"params":"[]","query":"BEGIN"}
debug: prisma:query {"duration":8,"params":"[\"127.0.0.1/32\",\"local\",\"2023-02-20 03:58:43.758 UTC\"]","query":"INSERT INTO \"data\".\"IP\" (\"address\",\"description\",\"updatedAt\") VALUES ($1,$2,$3) RETURNING \"data\".\"IP\".\"id\""}
debug: prisma:query {"duration":4,"params":"[]","query":"ROLLBACK"}
error: prisma:error 
Invalid `prisma.iP.create()` invocation:


Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Error { kind: ToSql(0), cause: Some(AddrParseError(Ip)) }), transient: false })

I think this one occurs because incorrect insert cmd: you have to add ::inet after the IP/CIDR. Something like

INSERT INTO "data"."IP" ("address","description","updatedAt")
VALUES ($1::inet,$2,$3)
RETURNING "data"."IP"."id"

As a final result it should be:

INSERT INTO "data"."IP" ("address","description","updatedAt")
VALUES ("127.0.0.1/32"::inet,"local","2023-02-20 03:58:43.758 UTC")
RETURNING "data"."IP"."id"

Also can anyone give a timeline for fixing those issues?

@janpio
Copy link
Member

janpio commented Feb 24, 2023

Please open a new issue for this unrelated problem @RrredHead - it is much easier for us to track these separately. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/bug A reported bug. topic: broken query topic: postgresql
Projects
None yet
Development

No branches or pull requests

3 participants