bonniss
bonniss
DTDrizzle Team
Created by Md Jahidul Islam milon on 7/9/2023 in #help
how to use insert with select in drizzle?
While waiting for new API in the PR, use sql.raw keep the field name as-is to make it work:
sql`INSERT INTO ${articleTags} (${sql.raw(articleTags.articleId.name)}, ${sql.raw(articleTags.tagId.name)})
SELECT ${upserted.id}, ${tags.id}
FROM ${tags}
WHERE ${tags.title} IN ${tagList};`
sql`INSERT INTO ${articleTags} (${sql.raw(articleTags.articleId.name)}, ${sql.raw(articleTags.tagId.name)})
SELECT ${upserted.id}, ${tags.id}
FROM ${tags}
WHERE ${tags.title} IN ${tagList};`
13 replies
DTDrizzle Team
Created by Md Jahidul Islam milon on 7/9/2023 in #help
how to use insert with select in drizzle?
wydm at "just use db.insert()"? select first then insert I guess?
13 replies
DTDrizzle Team
Created by Md Jahidul Islam milon on 7/9/2023 in #help
how to use insert with select in drizzle?
sql magic operator like:
sql`INSERT INTO ${articleTags} (${articleTags.articleId}, ${articleTags.tagId})
SELECT ${upserted.id}, ${tags.id}
FROM ${tags}
WHERE ${tags.title} IN ${tagList};`
sql`INSERT INTO ${articleTags} (${articleTags.articleId}, ${articleTags.tagId})
SELECT ${upserted.id}, ${tags.id}
FROM ${tags}
WHERE ${tags.title} IN ${tagList};`
generates query like:
INSERT INTO "article_tags" ("article_tags"."article_id", "article_tags"."tag_id")
SELECT $1, "tags"."id"
FROM "tags"
WHERE "tags"."title" IN ($2, $3);
-- params: [8, "tag1", "tag2"]
INSERT INTO "article_tags" ("article_tags"."article_id", "article_tags"."tag_id")
SELECT $1, "tags"."id"
FROM "tags"
WHERE "tags"."title" IN ($2, $3);
-- params: [8, "tag1", "tag2"]
which is invalid:
PostgresError: column "article_tags" of relation "article_tags" does not exist
PostgresError: column "article_tags" of relation "article_tags" does not exist
How to remove table prefix per field in table call?
13 replies