Comments (7)
Okay, @vatheara , I found the problem. This is a MySQL level constraint. According to MySQL's documentation, LAST_INSERT_ID()
is meant for returning the ID for a primary key that is an AUTO_INCREMENT
integer. Therefore, it makes sense that it will always be 0 because there is no auto-incrementing primary key column. I hope this helps!
from database-js.
To add to this, part of the benefits of UUIDs as well in practice is that you can generate the ID client side, which it appears you're doing.
It appears you're doing like:
const params = {id: uuid(), ...}
This uuid()
is be generated client side.
So you could either do like,
const userId = uuid();
const params = {id: userId};
Or just grab
const params = {id: uuid()};
...
const userId = params.id;
There's not particularly a reason to fetch this from the server. If the INSERT succeeded, you already have the ID you need.
And a last point, the syntax you're referring to, specifically RETURNING
is PostgreSQL syntax, not MySQL.
from database-js.
Hey there. you actually have an extra, unnecessary query in there. You can just run INSERT INTO
and then on the response, you can use the insertId
attribute that's returned on the object for the last inserted ID. Hope this helps!
from database-js.
By the way, here's a link to the code in question: https://github.com/planetscale/database-js/blob/main/src/index.ts#L34
If you do const rows = conn.execute(...)
, you can then do rows.insertId
to get the last inserted ID.
from database-js.
Thanks for reply
I also tried to use insertId
but it's always return 0 as value which is incorrect because i generate id from uuid()
anyway in my case i'm using this to get the inserted id which is I don't know if it's a good practice or not but it's work for me :
const id_query = "SELECT id FROM Conversations WHERE userId=:userId ORDER BY createdAt DESC LIMIT 1";
const id_params = {userId}
const conversationId = await conn.transaction(async (conn) => {
return await conn.execute(conversation_query, conversation_params).then(async() => {
return (await conn.execute(select_query,select_params)).rows[0];
})
})
note: i'm using "@planetscale/database": "^1.10.0", and the id doesn't have default value
from database-js.
Hmmm, okay that's interesting. We'll look into insertId not working with UUIDs, do you know if the equivalent works with regular MySQL?
from database-js.
Appreciate it thanks you!
from database-js.
Related Issues (20)
- Strongly type "type" property of Field
- Type error fetch failed HOT 10
- Automatically handle seeing a port 3306 in a connection string HOT 4
- INVISIBLE columns showing up with SELECT * HOT 6
- feat: Support syntax helper for "bulk insert with parameters"
- Malformed interface Field HOT 1
- [Feature] A type can be defined for `rows` in the query result HOT 6
- json_object converts bigints to numbers HOT 2
- Weird error with FKC HOT 5
- Error: direct DDL is disabled. Shadow Databases not helping, Development Branches not helping HOT 4
- New tables not found in foreign key enabled cluster HOT 2
- bug: `BLOB` / `Buffer` input parameters result in corrupted data
- Simple `show tables` statement not cast correctly by default HOT 11
- UUIDs Get Returned Incorrectly HOT 2
- CF Hyperdrive support? HOT 5
- Bug with JSON columns on >=1.15.0 HOT 12
- Customising cast results in unexpected behaviour HOT 2
- Numbers returned as strings HOT 15
- CI for tagging and pushing releases
- VT12001: unsupported: subqueries in DML when using delete() with where that filters on relation field HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from database-js.