home Cloud computing and code文章正文

system prompts successful upload of articles, but they are not stored in the database

Cloud computing and code 2026年02月02日 16:40 175 Pinwu
Z-Blog system uploads articles successfully, but the reason why there is no check-in is that the field 'log_images' in one of the tables does not have a default value.

This error usually occurs when inserting data into the database without providing a value for the field 'log_images', which is set to cannot be NULL in the database and has no default value.


There are several ways to fix this error:
1. When inserting data, provide a value for the 'log_images' field (it can be NULL, but the field needs to allow NULL).
2. Modify the database table structure to set a default value for the 'log_images' field.
3. If the field is redundant, consider removing it from the table structure (but it needs to be judged based on the actual situation).
log in to the database and use the sql statement: ALTER TABLE abc_post MODIFY COLUMN log_images VARCHAR(255) DEFAULT NULL
If the error prompt indicates that the maximum length is more than 255, such as 1000, then we need to change VARCHAR(255) to VARCHAR(1000) or a larger value, However, note that the maximum VARCHAR is 65535 (but it is generally recommended not to exceed 65535 due to the actual line size limit, and pay attention to the character set, each character is up to 4 bytes under utf8mb4).

Alternatively, if the field needs to store long strings, consider using the TEXT type.

there are two ways:
If the ultra-long data can be truncated, then truncate the ultra-long data first, and then modify the field.
modify the field to a larger length or use TEXT.
According to Z-Blog's design, log_images fields are often used to store images in articles, which may be one or more image URLs, separated by some kind of separator. 255 may not be enough, especially if there are multiple pictures.
Therefore, I recommend using the TEXT type, which allows longer strings to be stored.
modified to:
ALTER TABLE abc_post MODIFY COLUMN log_images TEXT DEFAULT NULL;
However, note that if the original field is already of type TEXT, then the error may not be a length issue, but something else.
So, we can try two options:
Scheme 1: Increase the length of VARCHAR (if the maximum length is not particularly large, e.g. no more than 10000)
Option 2: Change to TEXT
So, execute:
ALTER TABLE abc_post MODIFY COLUMN log_images TEXT DEFAULT NULL;
If you still get a data truncation error, it may be because the TEXT type has a maximum length limit (but TEXT can store 65535 bytes, usually sufficient).
If that doesn't work, there may be a very long string in the existing data that exceeds the maximum length of the TEXT (e.g. if the original field was LONGTEXT, but now it is TEXT, it may reduce the capacity). So we need to look at the type of the original field first.
view the original field type:
SHOW CREATE TABLE abc_post;

find the current type of log_images field from the output.
Assuming that the original field is already LONGTEXT, then we can't change it to TEXT, but need to keep LONGTEXT.
So, based on the original field type, we can:
if the original field is a VARCHAR (some length), then we can change it to TEXT or a larger VARCHAR.
If the original field is already of type TEXT or larger, then we can't minify it, but keep it as it is or larger.
So, in case of uncertainty, we can look at the original field type first.


標籤: field TEXT log_images

AmupuCopyright Amupu.Z-Blog.Some Rights Reserved.