question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Decimal value stored in MySql DB with floating point error

See original GitHub issue

Bug description

Decimal value stored with floating point error in mysqldb. Using a DECIMAL(20,10) field type in mysql and inserting the value 607013.31, in fact 607013.3100000001 is inserted. Checked the mysql logs and the error is already in the INSERT statement generated by prisma

How to reproduce

git clone https://github.com/unimatrix27/prismaissue cd prismaissue docker-compose up

This demo repo is a minimal environemt to demonstrate only this bug. Using docker-compose it sets up a mysql server, creates the demotable and runs NODE with the demoapp to show the bug

Expected behavior

The decimal value should be stored without floating point issues

Prisma information

prisma version 2.20, introspected DB, simplistic demo environment with no other code

Environment & setup

  • OS: Docker NODE:15
  • Database: MySql 5.7 (also tested 8.0)
  • Node.js 15
  • Prisma version: 2.20
const { PrismaClient } = require('@prisma/client')
var prisma =  new PrismaClient({ log: ['query', 'info', 'warn'] })
    
var enterTestData = prisma.demotable.create({
  data: {demofield: 607013.31}
    }).then(r => {
        console.log(r.demofield)
        // this console.log results in 607013.3100000001
    }
)
model demotable {
  id        Int      @id @default(autoincrement()) @db.UnsignedInt
  demofield Decimal? @db.Decimal(20, 10)
}

#6284

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
unimatrix27commented, Apr 2, 2021

upgraded to 2.20 also in the test repo, same result

1reaction
pimeyscommented, Apr 20, 2021

This is still super confusing. We’re having a meeting about floatish values tomorrow, hopefully we make it simpler to use in the future.

You’re not the first one having trouble… 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

B.3.4.8 Problems with Floating-Point Values
Floating -point numbers sometimes cause confusion because they are approximate and not stored as exact values. A floating-point value as written in an...
Read more >
Floating Point Rounding Errors in MySQL - Navicat
Floating -point means the decimal point can be placed anywhere relative to the significant digits of the number with the actual position being ......
Read more >
Floating point precision issue in PHP and MySQL for storing ...
Floats represent integers the way you want them, and they lose precision only when you store certain decimals. Share.
Read more >
SQL FLOAT: Avoid Weird Math Errors with These Top 3 Points
Article explains how to avoid weird math errors when working with SQL FLOAT data type. Follow our easy examples!
Read more >
Floating point numbers - Manual - PHP
So, if you add two floats, end up with a "special" value, e.g. 1.2E+6, then put that value unmodified into an update query...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found