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

MySQL insertId is undefined after executeTakeFirstOrThrow() #1049

Closed
Dan1ve opened this issue Jun 22, 2024 · 8 comments
Closed

MySQL insertId is undefined after executeTakeFirstOrThrow() #1049

Dan1ve opened this issue Jun 22, 2024 · 8 comments
Labels
api Related to library's API duplicate This issue or pull request already exists mysql Related to MySQL question Further information is requested sqlite Related to sqlite

Comments

@Dan1ve
Copy link

Dan1ve commented Jun 22, 2024

I'm trying to transform the following SQLite statement into the corresponding MySQL expression:

SQLite:

const insertedId = await this.db
	.insertInto('Vehicle')
	.values(vehicle)
	.onConflict((builder) => builder.doNothing())
	.returning('Vehicle.vehicleId')
	.executeTakeFirst();

I've replaced onConflict() with ignore() and removed returning() , as both of them aren't supported by the MySQL dialect. So now I have

MySQL:

const insert = await this.db.insertInto('Vehicle')
            .values(vehicle)
             .ignore()
             .executeTakeFirstOrThrow();
const insertedId  = insert.insertId;

My understanding is that insert.insertIdshould be the id of the inserted entry, but it is undefined .

Is there another way to retrieve the ID of the inserted entry? Re-querying it directly after the insertion is quite cumbersome.

Further details:

I'm using Prisma and prisma-kysely , if this is relevant here.

The vehiceId field is defined as follows in the schema.prisma :

vehiceId    String       @id @default(dbgenerated("(uuid())"))

PS: Thank you for creating such a nice library 🙌🏼

@Dan1ve Dan1ve closed this as completed Jun 22, 2024
@Dan1ve
Copy link
Author

Dan1ve commented Jun 22, 2024

Closed, likely a duplicate to #316 :

InsertId is irrelevant and may not be available when the table's primary key is not an auto increment column.

Also, the docs state:

This property can be undefined when the query contains an on conflict

Still a little bummer that the original query has to be split up into two, but oh well...

@igalklebanov igalklebanov added question Further information is requested mysql Related to MySQL sqlite Related to sqlite api Related to library's API duplicate This issue or pull request already exists labels Jun 22, 2024
@koskimas
Copy link
Member

Those are not our limitations but MySQL's. Feel free to take this up to MySQL.

@heethjain21
Copy link

heethjain21 commented Jun 29, 2024

Those are not our limitations but MySQL's. Feel free to take this up to MySQL.

Not sure if I understand the workings under the hood, but I do get a insertId when using mysql2, so maybe not a MySQL limitation

This is the response from mysql2:

    { rows: [], insertId: '0', rowsAffected: 1 }

And in Kysley, I get this:

InsertResult { insertId: undefined, numInsertedOrUpdatedRows: 1n } 

The docs mention this:

This property can be undefined when the query contains an on conflict clause that makes the query succeed even when nothing gets inserted.

In my case, there is

  • no conflict
  • the primary key has an auto increment param

So maybe not an issue with MySQL, something else

@JorgeJimenez15
Copy link

@heethjain21, Did you managed to find a workaround?

@heethjain21
Copy link

heethjain21 commented Jul 21, 2024

@heethjain21, Did you managed to find a workaround?

No. I was thinking of creating a PR to fix this, but haven't gotten time yet.

@koskimas could you please point me in the direction where I should be looking to fix this problem? Referring to this comment: #1049 (comment)

I'll start once I manage to get some time.

@koskimas
Copy link
Member

We have tests. This works. Provide a reproduction.

@heethjain21
Copy link

heethjain21 commented Jul 22, 2024

Strange, it worked this time.

Not sure what the issue was last time.

I wanted to adopt Kysely in our project, but didn't do it because of this error.

I tried a little more times, and it worked all of the time.

Don't remember what might have caused the issue last time.

Thanks for your comment anyways, since I was able to get it working now, so I can use it in my project then!

@tsulatsitamim
Copy link

tsulatsitamim commented Jul 24, 2024

Using retuning with query builder return undefined.

Screenshot 2024-07-24 at 10 04 51
Screenshot 2024-07-24 at 10 04 39

my workaround is using raw sql
const { rows: [row] } = await sql`INSERT INTO Transaction (accountId, amount, date, description, type) VALUES (${data.toAccountId}, ${data.amount}, ${data.date}, 'TRANSFER', 'DEBIT') RETURNING id`.execute(trx)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to library's API duplicate This issue or pull request already exists mysql Related to MySQL question Further information is requested sqlite Related to sqlite
Projects
None yet
Development

No branches or pull requests

6 participants