sea-schema icon indicating copy to clipboard operation
sea-schema copied to clipboard

Fail to distinguish MySQL default expression / value

Open billy1624 opened this issue 3 years ago • 2 comments

thread 'main' panicked at 'assertion failed: `(left == right)`

Diff < left / right > :
<"CREATE TABLE `order` ( `id` int NOT NULL AUTO_INCREMENT, `total` decimal(19, 4), `bakery_id` int NOT NULL, `customer_id` int NOT NULL, `placed_at` datetime NOT NULL DEFAULT '2021-11-05 00:00:00', KEY `FK_order_bakery` (`bakery_id`), KEY `FK_order_customer` (`customer_id`), PRIMARY KEY (`id`), CONSTRAINT `FK_order_bakery` FOREIGN KEY (`bakery_id`) REFERENCES `bakery` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `FK_order_customer` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci"
>"CREATE TABLE `order` ( `id` int NOT NULL AUTO_INCREMENT, `total` decimal(19, 4), `bakery_id` int NOT NULL, `customer_id` int NOT NULL, `placed_at` datetime NOT NULL DEFAULT 2021-11-05 00:00:00, KEY `FK_order_bakery` (`bakery_id`), KEY `FK_order_customer` (`customer_id`), PRIMARY KEY (`id`), CONSTRAINT `FK_order_bakery` FOREIGN KEY (`bakery_id`) REFERENCES `bakery` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `FK_order_customer` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci"

', tests/live/mysql/src/main.rs:56:9

billy1624 avatar Nov 05 '21 08:11 billy1624

Is it fixed now?

tyt2y3 avatar Feb 02 '22 04:02 tyt2y3

Not yet fixed

billy1624 avatar Feb 10 '22 10:02 billy1624

I was about to take on this issue as a first issue in the Rust world specifically, however, I have not been able to reproduce the issue.

This is what I've done:

Updated code

Added a .default() to the placed_at column of the order table with value "2021-11-05 00:00:00"

Snippet

 .col(
            ColumnDef::new(Alias::new("placed_at"))
                .date_time()
                .not_null()
                .default("2021-11-05 00:00:00"),  // <------------
        )

Result

The test exited with code 0, and below is the order table comparison.

Expected SQL:
CREATE TABLE `order` ( `id` int NOT NULL AUTO_INCREMENT, `total` decimal(19, 4) UNSIGNED, `bakery_id` int NOT NULL, `customer_id` int NOT NULL, `placed_at` datetime NOT NULL DEFAULT '2021-11-05 00:00:00', KEY `FK_order_bakery` (`bakery_id`), KEY `FK_order_customer` (`customer_id`), PRIMARY KEY (`id`), CONSTRAINT `FK_order_bakery` FOREIGN KEY (`bakery_id`) REFERENCES `bakery` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `FK_order_customer` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
Generated SQL:
CREATE TABLE `order` ( `id` int NOT NULL AUTO_INCREMENT, `total` decimal(19, 4) UNSIGNED, `bakery_id` int NOT NULL, `customer_id` int NOT NULL, `placed_at` datetime NOT NULL DEFAULT '2021-11-05 00:00:00', KEY `FK_order_bakery` (`bakery_id`), KEY `FK_order_customer` (`customer_id`), PRIMARY KEY (`id`), CONSTRAINT `FK_order_bakery` FOREIGN KEY (`bakery_id`) REFERENCES `bakery` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `FK_order_customer` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

Please let me know if the above steps validate that this issue no longer exists 😃

assemmarwan avatar Jan 09 '23 22:01 assemmarwan

Hey @assemmarwan, thanks for the initiative! How about the other way round? A default expression.

 .col(
            ColumnDef::new(Alias::new("placed_at"))
                .date_time()
                .not_null()
                .default(Expr::current_timestamp()),  // <------------
        )

billy1624 avatar Mar 23 '23 12:03 billy1624

the same issue, in mysql table a column has a default value "hello".

// want
`field1` varchar(64) NOT NULL DEFAULT "hello" COMMENT 'the field',
// but got 
`field1` varchar(64) NOT NULL DEFAULT hello COMMENT 'the field',

@tests/write/mysql/src/main.rs

I find may be crate sea-query @src/backend/table_builder.rs:97 cause this.

thinkgos avatar May 18 '23 01:05 thinkgos

@thinkgos Can you submit a reproducible example?

In particular, is the discovery result correct, or is the bug inside the writer?

tyt2y3 avatar May 19 '23 11:05 tyt2y3

Ping! I just created a PR, please check

  • https://github.com/SeaQL/sea-schema/pull/110

billy1624 avatar Jun 07 '23 08:06 billy1624