Error
Error Code:
3770
MySQL Error 3770: Disallowed Function in Default Value
Description
MySQL Error 3770 occurs when a column's `DEFAULT` value expression contains a function that is not permitted. MySQL has strict rules about which functions can be used in `DEFAULT` clauses during `CREATE TABLE` or `ALTER TABLE` operations, primarily allowing only literal values, `NULL`, or specific timestamp functions like `CURRENT_TIMESTAMP`.
Error Message
Default value expression of column '%s' contains a disallowed function: %s.
Known Causes
3 known causesUsing Non-Deterministic Functions
Attempting to use functions like `RAND()`, `UUID()`, or user-defined functions that produce different results on each call as a column's default value, which MySQL disallows.
Unsupported SQL Function
Employing standard SQL functions (e.g., `CONCAT()`, `SUBSTRING()`, arithmetic operations) in a `DEFAULT` clause, which MySQL restricts to a very limited set for column defaults.
Incorrect Default Syntax Expectation
Believing that any valid SQL expression or function can be used as a default value, without adhering to MySQL's specific and limited syntax for `DEFAULT` column definitions.
Solutions
3 solutions available1. Remove Disallowed Function from Default Value easy
Modify the table definition to exclude the problematic function from the default value.
1
Identify the column and the disallowed function causing the error. This information will be present in the error message.
2
Alter the table to remove the disallowed function from the default value. If the function was intended to provide a dynamic value, consider an alternative approach (see Solution 2).
ALTER TABLE your_table_name MODIFY COLUMN your_column_name data_type DEFAULT 'static_value';
-- Or, if you want to remove the default entirely:
ALTER TABLE your_table_name ALTER COLUMN your_column_name DROP DEFAULT;
3
If you are creating a new table, simply redefine the column without the disallowed function in its default value.
CREATE TABLE your_table_name (
your_column_name data_type DEFAULT 'static_value'
);
2. Use a Trigger for Dynamic Default Values medium
Implement a BEFORE INSERT trigger to set the dynamic value when a new row is inserted.
1
Identify the column and the disallowed function. Understand the desired dynamic behavior.
2
Create a `BEFORE INSERT` trigger that sets the column's value using the desired function. This bypasses the default value constraint.
DELIMITER $$
CREATE TRIGGER set_your_column_default
BEFORE INSERT ON your_table_name
FOR EACH ROW
BEGIN
IF NEW.your_column_name IS NULL THEN
SET NEW.your_column_name = YOUR_DISALLOWED_FUNCTION();
END IF;
END$$
DELIMITER ;
-- Note: Replace 'YOUR_DISALLOWED_FUNCTION()' with the actual function and its arguments. If the function is non-deterministic (e.g., NOW(), UUID()), ensure it's appropriate for triggers.
3
Ensure the column's default value is set to `NULL` or removed to allow the trigger to operate.
ALTER TABLE your_table_name MODIFY COLUMN your_column_name data_type DEFAULT NULL;
3. Update Application Logic to Handle Defaults medium
Modify your application code to provide the default value during inserts.
1
Identify the column and the disallowed function. Determine if the function's output is critical for the default behavior.
2
Remove the disallowed function from the column's default value in the MySQL schema.
ALTER TABLE your_table_name MODIFY COLUMN your_column_name data_type DEFAULT NULL; -- Or a static default if applicable
3
In your application code (e.g., Python, Java, PHP), when inserting new records, explicitly provide the value for the affected column using the logic that was previously in the default value.
// Example in Python with a hypothetical function
from datetime import datetime
def get_dynamic_value():
return datetime.now().strftime('%Y-%m-%d %H:%M:%S')
column_value = get_dynamic_value()
cursor.execute("INSERT INTO your_table_name (your_column_name, other_column) VALUES (%s, %s)", (column_value, 'some_other_value'))