How to push to an array?
I have a text array column, not nullable, and am having trouble pushing to the array in PG
and i get a very unhelpful error:
TypeError: value.map is not a function
31 Replies
Hello, @jakeleventhal! There are 2 options to push an element to array in pg:
@solo i just tried both. both give me "TypeError: value.map is not a function"
What driver do you use and can you share more code with your query? I just created new db, schema with your
arrayField
column, insert one row (arrayField
by default is []
) and then pushed an element using my code above, everything worked out. For this I used postgres-js
driver.related:
TypeError: Cannot read properties of undefined (reading 'map')
for jsonb
db.query.marketplaceIntegrations.findFirst({ where: sql
${marketplaceIntegrations.id} = 'asdfasdf'
}).toSQL() -> fails
db.query.marketplaceIntegrations.findFirst({ where: eq(marketplaceIntegrations.id, 'asdfasdf' }).toSQL() -> works
seems the issue is with the sql operatorFor me both queries worked out, I used your code above, just with
users
table, where id
has serial
type
sorry, I am a bit confused. What is not working for you right now? Cause firstly we were speaking about pushing an element to array and now we are speaking about another query:)well, i am trying to narrow down the issue, but it seems the root issue is the sql operator not working properly
because any query with the sql operator gives a similar error
Cannot read properties of undefined (reading 'map')
https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/alias.ts#L110do you use backticks with sql operator?
db.query.marketplaceIntegrations.findFirst({ where: sql${marketplaceIntegrations.id} = 'asdfasdf' }).toSQL() -> fails
here for example there is no `` with sql operator.yes it got formatted strangely
The root error is https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/alias.ts#L110
when i log
query
, i get this:
note that queryChunks
is not a field on query
, thus it fails on queryChunks.map
seems like a legitimate bugI found probably related issue if you are using Next
https://discord.com/channels/1043890932593987624/1166089749614825654
i am using next, but i also get this in an express node server
your code with my queries
@Angelelz could you please help to find out what is wrong?
Sure, give me a couple minutes
Let's start from the beginning, what is the query that you're trying to run? In SQL
simplest repro is:
"db.query.users.findFirst({ where: sql`${users.id} = 'asdf'` }).toSQL()"
What type of column is
users.id
?So just a refular text column
yeah
And you're obviously using pg right?
What driver?
all shown here^
Does it work with another driver?
my docker image
reproduced same issue with node-postgres
Ok, so it seems like the problem is with the postgres driver?
Alright one sec
i linked to the relevant line of failure above
You'll have to put together a reproduction repo
I can't reproduce
will do
ty for trying/looking
Are you sure you're importing sql from drizzle-orm?
I think postgres.js has an import with the same name
I saw somebody a long time ago with a similar issue, and it was because of this
ok holy smokes i figured it out
causes name conflict and prefers the prisma one
Uff, that line hurts my eyes
Why would you export * from drizzle?
this made more sense to do in prisma since that is where all your generated types live. i was mirroring this pattern for drizzle. youre right that it actually doesnt make sense to do that with drizzle though