"#1265 - Data truncated" error changing column (set) values - mysql

This error occurs several times, while I attent to change values in a "set" column, in structure mode of Phpmyadmin.
I get an "#1265 - Data truncated" error, even if the new length of a value is exactly the same than the old one (for example if I change "fra" with "frn", or "gre" with "grc").
I.g. I have a "lang" column (in my table quotations), whose values are: 'it','lat','fr','en','deu','esp','gr'.
I cannot change not only 'fr' with 'frn' or 'gr' with 'grc', but neither 'lat' with 'ltn'. I don't understand why.
I have PHP version: 7.3.7, MariaDB: 10.3.16-MariaDB, and Phpmyadmin: 4.9.4
Thanks

I found the solution (quite trivial, indeed): the problem was that some records were already with the value I would change.
So, instead than replace 'gr' with 'grc', I added 'grc' in table structure. Then I searched all my items with 'gr' value and changed them with 'grc' value.
And finally I deleted 'gr' from table structure.

Related

MySQL ERROR 1265: Data truncated for column

I can't figure out why I'm getting this message. I'm using MySQL Workbench and am editing the values in an ENUM field that connects to a dropdown choice in my app.
Everything seems to be fine. I've searched on this error and all I find refers to datatype mismatches but, in this instance, that's not possible with ENUM when feeding it an array of string values.
Here's the SQL
Executing:
ALTER TABLE `mydbase`.`average_monthly_expenses`
CHANGE COLUMN `expense_category` `expense_category` ENUM('Home', 'Healthcare', 'Child care', 'Groceries and supplies', 'Eating out', 'Utilities', 'Telecomms', 'Laundry and cleaning', 'Clothes', 'Education', 'Entertainment gifts vacation', 'Auto and transportation', 'Insurance', 'Savings and investments', 'Charitable contributions', 'Itemized monthly payments') NULL DEFAULT NULL ;
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1265: Data truncated for column 'expense_category' at row 1
SQL Statement:
ALTER TABLE `mydbase`.`average_monthly_expenses`
CHANGE COLUMN `expense_category` `expense_category` ENUM('Home', 'Healthcare', 'Child care', 'Groceries and supplies', 'Eating out', 'Utilities', 'Telecomms', 'Laundry and cleaning', 'Clothes', 'Education', 'Entertainment gifts vacation', 'Auto and transportation', 'Insurance', 'Savings and investments', 'Charitable contributions', 'Itemized monthly payments') NULL DEFAULT NULL
Any suggestions are very welcome
The query itself is correct.
modelling fiddle
Execute
SELECT DISTINCT expense_category, HEX(expense_category)
FROM mydbase.average_monthly_expenses
and check the output for the values which are not listed in the column definition.
There may be typos, leading/trailing spaces or another non-printed symbols, double spaces in the middle of the value, or there may be some collation problems.
UPDATE
My current field definition I'm trying to change to the above is ENUM('Home', 'Living', 'Telecommunications', 'Transportation', 'Other'). When I run your suggested SQL I just get Housing and Other listed.
These values are absent in new column definition - so server cannot convert them and truncates the values.
Recommendations: alter column definition, add new values to ENUM values list but do not remove old ones; update table and replace old values with new ones; alter column definition and remove old values from the list.
#Akina
So I figured out why I was blocked from editing the values. It would not let me make any edits that changed either value "Housing" or "Other". I did as you suggested, adding my new values to the existing ones, no problem, that worked fine. I couldn't however delete "Housing" or "Other", but the other prior values deleted fine. For the moment, I kept both, using "Housing" instead of "Home" and leaving "Other" at the end.
But I wanted to know however why those two values were protected, and then it dawned on me, there were existing records using those values. I manually changed all instances using "Other" to "Telecomms" and then I could remove "Other" from the ENUM values. All good now.

Unable to change the value of enum field in MySQL when manually typing a query

I have an enum column in a MySQL table called site_status:
From my understanding, updating the value of such a field is exactly the same process as changing value of a varchar or int or any other field.
The only "limitation" is that as a value I obviously have to set one from predefined values - as this is the nature of enum types. Right?
So I want to change site_status from LIVE to FREE:
I type:
update db_name.db_table set site_status = 'FREE' where site_id = 501;
Pretty easy query. Unfortunately does nothing. It also doesn't produce any error. Simply:
0 rows affected.
What am I doing wrong? When I manually click via GUI in Sequel Pro, the value is changed to FREE with no issues. I see a dropdown from which I pick another option.

why ami getting this #1305 - FUNCTION homeshopping.partID does not exist

mysql php admin table query enter image description here
I don't see why it says partID is giving me a problem? it is in the table and i think i have them linked correctly. I Have changed a few things i replaced comma with the and statement which cleared up alot of my errors. But since I've done that it continues to give me #1305 - FUNCTION homeshopping.partID does not exist. I have even looked at the structures an designer to make sure column names an tables are correct.
Don't forget to post your query as text also.
Because it's missing the keywork IN to have a query like this:
SELECT CONCAT(hscust.first,' ', hscust.last AS Customer,hsitems.description,hsitems.price,hsitems.itemCalss
FROM hscust,hsorders,hslineitem,hsitems
WHERE hslineitems.orderId = hsorders.orderId AND hsitems.partID = hslineitem.partNum AND hslineitem.price = hsitems.price AND partID IN ('CB03', 'CZ82');

Trying to fix "tMySQL query error: Duplicate entry" For a garrysmod server

I'm new to this website, and I'm not sure if I'm supposed to ask questions about mysql things in a game server. Anyways, I'm trying to fix this error here, and I was wondering if there's anything I can do to figure out, or solve where the issue is occurring.
Trace:
1: Line 32 "Trace" lua/includes/extensions/debug.lua
2: Line 33 "nil" gamemodes/catalystrp/gamemode/data.lua
3 C function
4: Line 84 "nil" lua/includes/modules/hook.lua
tMySQL query error: Duplicate entry 'STEAM_0:0:53698800' for key 'PRIMARY'Query:
INSERT INTO metrorp_wallets (`steam`, `rpname`) VALUES('STEAM_0:0:53698800', 'T
what this error means is that you are trying to insert a row to the table that conflicts with row already in the table (meaning- there's already a row in the table with the same primary key).
How to deal with this case is really a matter of what you are trying to do. I can think of three ways to go here:
if this case is illegal, than I guess you should not change the query, but handle the error (exception, log, whatever)
if you want to replace the data in the table with the new row, use REPLACE INTO instead of INSERT INTO in your query.
if you want to keep the data that's already there, use INSERT IGNORE in your query.

Table.column_names returns old values in a migration

I'm doing a migration where I rename the columns: currency becomes legacy_currency etc. The problem is that when I'm trying to get the column names for the table, it returns old values.
Tried using reset_column_information and it didn't work. Also tried the following:
Model.connection.schema_cache.clear!
Model.reset_column_information
Doesn't seem to work. Any suggestions?
An excerpt from the code:
currency_columns = table_class.column_names.grep(/currency/).map(&:to_sym)
currency_columns.each do |currency_col|
rename_column table, col, "legacy_#{col}".to_sym
end
...
table_class.column_names #still has the old values
Since I've investigated that the issue only appeared on one of the models, I've debugged it and found that we were using an old(ish) version of acts-as-taggable-on (3.3.0), which used cache for the columns method, which was called by column_names in ActiveRecord.
Updating the gem to 3.5.0 resolved the issue.