Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default value of string like field can be empty string "" or "null" #626

Merged
merged 2 commits into from
Mar 6, 2022

Conversation

fishead
Copy link
Contributor

@fishead fishead commented Mar 6, 2022

fix issue #625

@fishead fishead changed the title fix: Default value migration #625 Default value of string like field can be empty string "" or "null" Mar 6, 2022
@a8m
Copy link
Member

a8m commented Mar 6, 2022

Looks good! Thanks for the contribution @fishead ❤️ 🙏

@a8m
Copy link
Member

a8m commented Mar 6, 2022

I'll check why the CI is failing and will update you.

@a8m
Copy link
Member

a8m commented Mar 6, 2022

MariaDB renders the NULL default values as NULL string, I'll dig into this and will update. See COLUMN_DEFAULT and COLUMN_DEFAULT IS NULL:

SELECT `TABLE_NAME`, `COLUMN_NAME`, `COLUMN_TYPE`, `COLUMN_COMMENT`, `IS_NULLABLE`, `COLUMN_KEY`, `COLUMN_DEFAULT` IS NULL, `COLUMN_DEFAULT`, `EXTRA`, `CHARACTER_SET_NAME`, `COLLATION_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = 'test' AND `TABLE_NAME` = 'pets' ORDER BY `ORDINAL_POSITION`;
+------------+-------------+-------------+----------------+-------------+------------+--------------------------+----------------+-------+--------------------+----------------+
| TABLE_NAME | COLUMN_NAME | COLUMN_TYPE | COLUMN_COMMENT | IS_NULLABLE | COLUMN_KEY | `COLUMN_DEFAULT` IS NULL | COLUMN_DEFAULT | EXTRA | CHARACTER_SET_NAME | COLLATION_NAME |
+------------+-------------+-------------+----------------+-------------+------------+--------------------------+----------------+-------+--------------------+----------------+
| pets       | id          | bigint(20)  |                | NO          | PRI        |                        1 | NULL           |       | NULL               | NULL           |
| pets       | owner_id    | bigint(20)  |                | YES         | MUL        |                        0 | NULL           |       | NULL               | NULL           |
+------------+-------------+-------------+----------------+-------------+------------+--------------------------+----------------+-------+--------------------+----------------+

@a8m
Copy link
Member

a8m commented Mar 6, 2022

OK, just checked that on https://mariadb.com/kb/en/information-schema-columns-table/ and this patch should solve the issue. Please, add it to the mysql/inspect.go file:

 // marDefaultExpr returns the correct schema.Expr based on the column attributes for MariaDB.
 func (i *inspect) marDefaultExpr(c *schema.Column, x string) schema.Expr {
+       // Unlike MySQL, NULL means default to NULL or no default.
+       if x == "NULL" {
+               return nil
+       }
        // From MariaDB 10.2.7, string-based literals are quoted to distinguish them from expressions.
        if i.gteV("10.2.7") && sqlx.IsQuoted(x, '\'') {

@a8m
Copy link
Member

a8m commented Mar 6, 2022

@fishead, you're also welcome to our atlas community on Discord 😃
https://discord.com/invite/QhsmBAWzrC

@fishead
Copy link
Contributor Author

fishead commented Mar 6, 2022

@a8m PR updated. Thanks for the detail.

@a8m a8m merged commit 11c0af3 into ariga:master Mar 6, 2022
@a8m
Copy link
Member

a8m commented Mar 6, 2022

💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants