Zero timestamp value not accepted when using MySQL .NET connector - mysql

We have a C# tool to create a (MySQL) schema on a developer machine based on the (MySQL) schema used in production. This tool runs SHOW CREATE TABLE queries on the source connection, grab the resulting table creation scripts and run them locally.
I'm using MySQL 5.7.7 Community Server locally.
One of the creation script looks like this:
CREATE TABLE `agency` (
`id` int(11) unsigned NOT NULL,
`name` varchar(512) COLLATE utf8_unicode_ci NOT NULL,
`url` varchar(512) CHARACTER SET ascii NOT NULL,
`location_path` varchar(10) CHARACTER SET ascii NOT NULL,
`email_pattern` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`generic_email` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`address` text COLLATE utf8_unicode_ci,
`zip_code` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`city` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`region` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`country` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`phone` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`inserted_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
When I run this query in MySQL Workbench, the table is successfully created.
However, when this query is executed on the destination connection by our C# tool, the following error occurs: Failure: Invalid default value for 'inserted_at'.
My MySQL Workbench is configured with the following SQL_MODE: TRADITIONAL,ALLOW_INVALID_DATES.
My MySQL Server instance is configured with the following sql-mode: STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Here are the connection strings used by our C# tool:
Local connection string:
Server=localhost;Database=XXX;Uid=XXX;Pwd=XXX;AllowZeroDateTime=true
Source connection string:
Server=X.X.X.X;Database=XXX;Uid=XXX;Pwd=XXX;Use Compression=true;AllowZeroDateTime=true
Any idea appreciated!

It looks like removing STRICT_TRANS_TABLES from sql-mode in my.ini fixes the problem.
Is this the correct fix?

Related

Error database import from localhost to server

I want to upload database from localhost to server and I get 1067 error.
Localhost Mysql version - 5.7
Server - MariaDB, v5.5.68
Code part
CREATE TABLE `wp_momopay_payments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`order_id` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT '',
`amount` int(10) unsigned DEFAULT '0',
`phone_number` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT '',
`created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`payment_id` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT '',
`failed_reason` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT '',
`salt` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT '',
`status` varchar(36) COLLATE utf8mb4_unicode_ci DEFAULT 'pending',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Error message
ERROR 1067 (42000) at line 1721: Invalid default value for 'created'
program 'mysql' finished with non-zero exit code: 1
Mariadb 5.5 only allows TIMESTAMP, not DATETIME, columns to have the CURRENT_TIMESTAMP default value. So your table definition uses a feature it lacks.
Mariadb 5.5 hit end-of-life on 11-April-2020, well over two years ago.

Shopware import Database runs on "GENERATED ALWAYS AS" error

So i exported a shopware database using phpmyadmin with MySQL and get an "GENERATED ALWAYS AS" error on the order table when importing to a new database.
After a short time I get this error and can't find any solution to this.
I even deleted all orders to be sure there is no malformed order in my table.
This is what phpmyadmin returns:
SQL-Befehl:
CREATE TABLE `order` ( `id` binary(16) NOT NULL, `version_id` binary(16) NOT NULL, `state_id` binary(16) NOT NULL, `auto_increment` bigint(20) UNSIGNED NOT NULL, `order_number` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `currency_id` binary(16) NOT NULL, `language_id` binary(16) NOT NULL, `currency_factor` double DEFAULT NULL, `sales_channel_id` binary(16) NOT NULL, `billing_address_id` binary(16) NOT NULL, `billing_address_version_id` binary(16) NOT NULL, `price` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`price`)), `order_date_time` datetime(3) NOT NULL, `order_date` date GENERATED ALWAYS AS (cast(`order_date_time` as date)) STORED, `amount_total` double GENERATED ALWAYS AS (json_unquote(json_extract(`price`,'$.totalPrice'))) VIRTUAL, `amount_net` double GENERATED ALWAYS AS (json_unquote(json_extract(`price`,'$.netPrice'))) VIRTUAL, `position_price` double GENERATED ALWAYS AS (json_unquote(json_extract(`price`,'$.positionPrice'))) VIRTUAL, `tax_status` varchar(255) COLLATE utf8mb4_unicode_ci GENERATED ALWAYS AS (json_unquote(json_extract(`price`,'$.taxStatus'))) VIRTUAL, `shipping_costs` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`shipping_costs`)), `shipping_total` double GENERATED ALWAYS AS (json_unquote(json_extract(`shipping_costs`,'$.totalPrice'))) VIRTUAL, `deep_link_code` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `custom_fields` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`custom_fields`)), `affiliate_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `campaign_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `customer_comment` longtext COLLATE utf8mb4_unicode_ci DEFAULT NULL, `created_at` datetime(3) NOT NULL, `updated_at` datetime(3) DEFAULT NULL, `item_rounding` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`item_rounding`)), `total_rounding` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`total_rounding`)), `rule_ids` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`rule_ids`)), `created_by_id` binary(16) DEFAULT NULL, `updated_by_id` binary(16) DEFAULT NULL )
MySQL meldet: Dokumentation
#1064 - Fehler in der SQL-Syntax. Bitte die korrekte Syntax im Handbuch nachschlagen bei 'GENERATED ALWAYS AS (json_unquote(json_extract(`price`,'$.taxStatus'))) VIRTU...' in Zeile 19
Using this line of code via SSH on my dump, made it possible to Import it afterwards:
sed -i 's/COLLATE.*GENERATED ALWAYS/GENERATED ALWAYS/g' dump.sql
You can look into https://sw-cli.fos.gg/commands/project/#shopware-cli-project-dump-database and use that for the dump creation. It's a replacement for mysqldump and considers this all issues.
Otherwise, you need the same mysqldump/mysql cli binary as the server. Importing mostly works only in the same database type (mysql/mariadb)

Can not create table in phpmyadmin

I have removed users table in my database and imported another users table.
It show this error:
Error
SQL query:
CREATE TABLE `users` (
`id` int(11) UNSIGNED NOT NULL,
`user_group_id` int(11) UNSIGNED NOT NULL,
`gender` tinyint(1) DEFAULT NULL,
`first_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`last_name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`national_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`birthday` date DEFAULT NULL,
`company` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`eco_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`national_id` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`address` text COLLATE utf8mb4_unicode_ci,
`post_code` varchar(255) COLLATE utf8mb4_unicode_ci[...]
MySQL said: Documentation
#1215 - Cannot add foreign key constraint
My framwork is Laravel 5.6, PHP 7.2
How to solve this issue?
When you are working with migrations, you have to make every change in database through migrations. You can make changes in the existing users_table migration file. You can drop/add column/table using artisan command. Migrations in Laravel can do every interaction with the database. Take your time to go thorugh all the basics of migrations if you don't know:
Laravel Migrations
Use migration in laravel. It is better that way and check the migration field in your table. May be the previous migrations will still be there

Yii2 encrypt and Mysql

I hope someone can help me with this.
I am saving a personal data into MYSQL by encrypting using Yii2 encrypt/decrypt function, testing without saving the content into the DB works.
However saving the content into the DB and retrieving it back, seems something goes wrong because I am not able to decrypt it.
My MYSQL scheme is this
CREATE TABLE IF NOT EXISTS `customer` (
`customer_id` int(11) NOT NULL,
`customerHash` varchar(32) COLLATE utf8_bin DEFAULT NULL,
`customerDate` date DEFAULT NULL,
`customerTime` time DEFAULT NULL,
`customerUpdated` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`customerEmail` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`customerPassword` varchar(45) COLLATE utf8_bin DEFAULT NULL,
`customerToken` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`customerAddress` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`customerPhone` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`customerPostcode` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`customerName` blob,
`customerSurname` varchar(255) COLLATE utf8_bin DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Yii code
Encrypt
$request=Yii::$app->request;
$hash=Yii::$app->getSecurity()->generateRandomString();
$model->customerHash=$hash;
$model->customerName=Yii::$app->getSecurity()->encryptByKey($request->post('customerName'), $hash);
Decrypt
<? echo"Decode : ".Yii::$app->getSecurity()->decryptByKey($model->customerName, $model->customerHash);?>
Any help welcome.
Thanks

Problem with ColdFusion communicating with MySQL database

I have been working to migrate a non-profit website from a local server (running Windows XP) to a GoDaddy hosting account (running Linux). Most of the pages are written in ColdFusion. Things have gone smoothly, up until this point. There is a flash form within the site (see this page: http://www.preservenet.cornell.edu/employ/submitjob.cfm) which, when completed, takes the visitor to this page: submitjobaction.cfm . I'm not quite sure what to make of this error, since I copied exactly what had been in the old MySQL database, and the .cfm files are exactly as they had been when they worked on the old server. Am I missing something?
Below is the code from the database that the error seems to be referring to. When I change "Positionlat" to some default value in the database as it suggests in the error, it says that another field needs a default value, and it's a neverending chain of errors as I try to correct it.
This is probably a stupid error that I'm missing, but I've been working at it for days and can't find what I'm missing. I would really appreciate any help.
Thanks!
-Greg
DROP TABLE IF EXISTS `employopp`;
CREATE TABLE `employopp` (
`POSTID` int(10) NOT NULL auto_increment,
`USERID` varchar(10) collate latin1_general_ci default NULL,
`STATUS` varchar(10) collate latin1_general_ci NOT NULL default 'ACTIVE',
`TYPE` varchar(50) collate latin1_general_ci default 'professional',
`JOBTITLE` varchar(70) collate latin1_general_ci default NULL,
`NUMBER` varchar(30) collate latin1_general_ci default NULL,
`SALARY` varchar(40) collate latin1_general_ci default NULL,
`ORGNAME` varchar(70) collate latin1_general_ci default NULL,
`DEPTNAME` varchar(70) collate latin1_general_ci default NULL,
`ORGDETAILS` mediumtext character set utf8 collate utf8_unicode_ci,
`ORGWEBSITE` varchar(200) collate latin1_general_ci default NULL,
`ADDRESS` varchar(60) collate latin1_general_ci default 'none given',
`ADDRESS2` varchar(60) collate latin1_general_ci default NULL,
`CITY` varchar(30) collate latin1_general_ci default NULL,
`STATE` varchar(30) collate latin1_general_ci default NULL,
`COUNTRY` varchar(3) collate latin1_general_ci default 'USA',
`POSTALCODE` varchar(10) collate latin1_general_ci default NULL,
`EMAIL` varchar(75) collate latin1_general_ci default NULL,
`NOMAIL` varchar(5) collate latin1_general_ci default NULL,
`PHONE` varchar(20) collate latin1_general_ci default NULL,
`FAX` varchar(20) collate latin1_general_ci default NULL,
`WEBSITE` varchar(200) collate latin1_general_ci default NULL,
`POSTDATE` varchar(10) collate latin1_general_ci default NULL,
`POSTUNTIL` varchar(20) collate latin1_general_ci default 'select date',
`POSTUNTILFILLED` varchar(20) collate latin1_general_ci NOT NULL default 'until filled',
`texteHTML` mediumtext character set utf8 collate utf8_unicode_ci,
`HOWTOAPPLY` mediumtext character set utf8 collate utf8_unicode_ci,
`CONFIRSTNM` varchar(30) collate latin1_general_ci default NULL,
`CONLASTNM` varchar(60) collate latin1_general_ci default NULL,
`POSITIONCITY` varchar(30) collate latin1_general_ci default NULL,
`POSITIONSTATE` varchar(30) collate latin1_general_ci default NULL,
`POSITIONCOUNTRY` varchar(3) collate latin1_general_ci default 'USA',
`POSITIONLAT` varchar(50) collate latin1_general_ci NOT NULL,
`POSITIONLNG` varchar(50) collate latin1_general_ci NOT NULL,
PRIMARY KEY (`POSTID`)
) ENGINE=MyISAM AUTO_INCREMENT=2007 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
UPDATE:
Where I think the "submitjobaction.cfm" page communicates with the database:
<CFINSERT DATASOURCE="mysqlcf_preservenet" TABLENAME="employopp" FORMFIELDS=" TYPE, JOBTITLE, NUMBER, SALARY, ORGNAME, DEPTNAME, ORGDETAILS, ORGWEBSITE, ADDRESS, ADDRESS2, CITY, STATE, COUNTRY, POSTALCODE, EMAIL, NOMAIL, PHONE, FAX, WEBSITE, POSTDATE, POSTUNTIL, texteHTML, HOWTOAPPLY, CONFIRSTNM, CONLASTNM, POSITIONCITY, POSITIONSTATE, POSITIONCOUNTRY">
<CFINSERT DATASOURCE="mysqlcf_preservenet" TABLENAME="user" FORMFIELDS=" ORGNAME, WEBSITE, ADDRESS, ADDRESS2, CITY, STATE, COUNTRY, POSTALCODE, EMAIL, PHONE, FAX, CONFIRSTNM, CONLASTNM" >
I use none of mysql, cfform or cfinsert so ymmv with this answer, but it seems like the problem is in the database configuration.
This blog post from 2007 suggests changing an ini setting for sql-mode. You'll need to talk to your host to check the current value and try to have it changed.
It looks like the form is sending zero-length values for unanswered fields and the database is rejecting those values.
Another approach is to replace the cfinsert with a normal cfquery - this will give you more flexibility with how you supply 'empty' values.
I get the following error message:
Error Executing Database Query.
Field 'CONFIRSTNM' doesn't have a default value
Resources:
...
What seems to happen here is that some form field (named CONFIRSTNM - I assume that's the Contact Firstname) is empty in the POST and the database doesn't have a default value set. Because your cf code is using CFINSERT you don't actually write the SQL code yourself but CF is supposed to do it for you. Hard to debug from the distance, but I think what you should do is to make sure the database was 1:1 migrated from the old environment to the new environment. Not sure how the migration was done, but it might have lost some information along the way.
Another suggestion for good coding practice is to rewrite CFINSERT with a CFQUERY tag and write the SQL statement yourself. Also make sure you use CFQUERYPARAM for all incoming parameters.