-
-
Notifications
You must be signed in to change notification settings - Fork 525
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
last_insert_id
gives 0 when 0 is explicitly specified for an auto_increment
primary key in an insertion
#8769
Comments
Hi @ko41, thanks for taking the time to report this discrepancy with MySQL's behavior. I've moved this issue over to the I am able to reproduce the behavior you're reporting with Let me know if I misunderstood anything in there. We'll see if we can get someone to dig into this fix. |
Hi @ko41, thanks again for reporting this issue. 🙏 I've fixed the bug in the Let us know if you find any other issues! |
Thank you for looking into this issue @fulghum After digging around a bit more, I noticed that in MySQL,
are not identical when a non-zero primary key is specified (which is not emulated by Dolt's latest go-mysql-server).
package main
import (
"database/sql"
"fmt"
"github.com/go-sql-driver/mysql"
)
func main() {
config := mysql.NewConfig()
config.Net = "tcp"
config.Addr = ":3306"
config.DBName = "test"
config.User = "root"
config.Passwd = "root"
db, err := sql.Open("mysql", config.FormatDSN())
if err != nil {
panic(err)
}
if _, err := db.Exec(`DROP TABLE IF EXISTS auto_increment_tbl`); err != nil {
panic(err)
}
if _, err := db.Exec(`CREATE TABLE auto_increment_tbl (pk bigint NOT NULL AUTO_INCREMENT, c0 bigint, PRIMARY KEY (pk))`); err != nil {
panic(err)
}
result, err := db.Exec(`INSERT INTO auto_increment_tbl VALUES (10, 44)`)
if err != nil {
panic(err)
}
lastInsertIdFromOkPacket, err := result.LastInsertId()
if err != nil {
panic(err)
}
// the ok packet says that last_insert_id is 10
fmt.Printf("last_insert_id from ok packet: %d\n", lastInsertIdFromOkPacket)
var lastInsertIdFromQuery int
if err := db.QueryRow(`SELECT last_insert_id()`).Scan(&lastInsertIdFromQuery); err != nil {
panic(err)
}
// while the query says that last_insert_id is 0
fmt.Printf("last_insert_id from query: %d\n", lastInsertIdFromQuery)
} go-mysql-server used to behave the same as MySQL (confirmed with Could you kindly look into this? Thank you 🙏 |
As is described in this example,
https://github.com/dolthub/go-mysql-server/blob/729f39b6b665932cb0da516d5013f0d938c860d0/enginetest/queries/insert_queries.go#L559-L567
When 0 is specified explicitly,
InsertID
.This is not consistent with MySQL's behaviour, which can be replicated as follows:
The text was updated successfully, but these errors were encountered: