Wedge

Public area => Bug reports => The Pub => Archived fixes => Topic started by: live627 on October 7th, 2012, 02:02 AM

Title: Database column type 'decimal' ignored
Post by: live627 on October 7th, 2012, 02:02 AM
In plugin-info.xml, if there is a column typed 'decimal', it is ignored. Since it is a valid type in MySQL, shouldn't it be added to the list of allowed database types?
Title: Re: Database column type 'decimal' ignored
Post by: Arantor on October 7th, 2012, 02:12 AM
Who uses the bastardised format that is decimal? (Or, numeric, as it is also known)

I don't want to support it because I saw absolutely no good reason to extend everything in all the relevant places to extend a datatype that is not even consistent between MySQL versions, but if that weren't enough, it isn't even necessarily consistent between the same version of MySQL on different operating systems, and in some cases even between different table types on the same system.

If you are using it, the chances are you're really using the wrong data type and should be using something else.
Title: Re: Database column type 'decimal' ignored
Post by: live627 on October 7th, 2012, 02:16 AM
What would you recommend instead? Float or double?
Title: Re: Database column type 'decimal' ignored
Post by: Arantor on October 7th, 2012, 02:23 AM
Better question: what are you trying to do?
Title: Re: Database column type 'decimal' ignored
Post by: live627 on October 7th, 2012, 02:33 AM
Store a floated number, such as 12.5839. Latitude and longitude values from Google Maps.
Title: Re: Database column type 'decimal' ignored
Post by: Arantor on October 7th, 2012, 02:45 AM
Ah, you clearly don't read TheDailyWTF, because you'd know the answer straight away ;) :P

There is one simple rule when dealing with data like this and it is in the form of a yes/no question. Do you ever need to do manipulation on it as if it is a number? If the answer to this question is yes, find a suitable numeric column. If the answer to this question is no, make it a string and be done with it.[1]

Just because something takes the form of a number does not inherently mean you should store it as one. Seems to me that you're really recording data from Google Maps that just happens to be in the form of a number, and that you don't need to perform any maths on it (e.g. distance location) in which case the correct type would be a string.

Making it a string has other advantages - you never have a loss of precision. What column size would you have used for example? (Off the top of my head, I seem to recall you'd have to make it DECIMAL(13, 10) - but that's great until Google changes the resolution at which they're tracking)
 1. The canonical examples that TDWTF brings up are things like 5-digit PIN numbers or phone numbers, just because they look numeric...
Title: Re: Database column type 'decimal' ignored
Post by: live627 on October 7th, 2012, 02:57 AM
I'm using FLOAT(18, 15) at the moment
Title: Re: Database column type 'decimal' ignored
Post by: Arantor on October 7th, 2012, 02:59 AM
Float is inherently unreliable beyond a certain point (2^52 for purely whole numbers, but it depends what you're trying to store)

You probably could get away with floats if you were really pushed, but the correct thing to do is use a varchar unless you're planning on doing something that expressly needs it as a number like sorting or searching where distance is a factor.