Replies: 3 comments 4 replies
-
ok figured it out by splitting up the fields and just taking off one of the unique constraints...seems kinda hacky tho: // a. upsert anything that has the same oauth id
if (validatedUser.userId !== undefined){ // logged in, update on oauth id:
updateVote = tx.insert(formVotesTable)
.values({...formVotesData, createdAt: loadDate})
.onConflictDoUpdate({
target: [formVotesTable.formId, formVotesTable.oauthId],
set: {...formVotesData, updatedAt: loadDate}
}).returning()
// b. upsert anything that has the same remote ip - unless it has an oauth
} else {
updateVote = tx.insert(formVotesTable)
.values({...formVotesData, createdAt: loadDate})
.onConflictDoUpdate({
target: [formVotesTable.formId, formVotesTable.remoteIp],
set: {...formVotesData, updatedAt: loadDate}
}).returning()
};
const voteUpsert = await updateVote.values();
if (voteUpsert !== undefined){
console.log('VOTES(step 3): ',voteUpsert[0]) |
Beta Was this translation helpful? Give feedback.
-
here's what i ended up doing:
(had to change field names to make life easier on the js side) drizzle:
issue came down to not be able to have two constraints, so i just made one constraint on three fields when i try it with orm:
instead of updating the rows, it actually creates new ones... |
Beta Was this translation helpful? Give feedback.
-
Sorry to bother you, but it seems I also cannot determine whether to insert based on the composite index, and an error may occur: await db
.insert(modListUser)
.values({
id: '123',
modListId,
twitterUserId: 'twitter-user-1',
})
.onConflictDoNothing({
target: [modListUser.modListId, modListUser.twitterUserId],
}) schema.ts export const modListUser = sqliteTable(
'ModListUser',
{
id: text('id').primaryKey().$defaultFn(ulid),
modListId: text('modListId')
.references(() => modList.id)
.notNull(),
twitterUserId: text('twitterUserId')
.references(() => user.id)
.notNull(),
createdAt: text('createdAt')
.notNull()
.$defaultFn(() => new Date().toISOString()),
updatedAt: text('updatedAt')
.notNull()
.$defaultFn(() => new Date().toISOString()),
},
() => [modListUserUnique],
)
export const modListUserUnique = uniqueIndex('modListUser_unique').on(
modList.id,
modListUser.twitterUserId,
) |
Beta Was this translation helpful? Give feedback.
-
i have the following in my schema:
i tried to do an upsert using the two fields:
but then i get:
so wondering if i can just do something like
target: formVotesTable.unq
and to also have multiple targets, like this:
my goal is to be insert data if it doesn't exist, and update it with different data if it does exist...
Beta Was this translation helpful? Give feedback.
All reactions