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

Support expressions (functions) for field defaults (table definition), ALTER, INSERT, CREATE? #14371

Open
OlafvdSpek opened this issue Jun 8, 2018 · 53 comments · May be fixed by #15975
Open
Labels
affects/5.2 This issue or pull-request affects 5.2.x releases (and maybe further versions) affects/6.0 This issue or pull-request affects 6.0.x releases (and maybe further versions) Bug A problem or regression with an existing feature confirmed/5.2 This issue is confirmed to be reproduced on 5.2 at the time this label was set confirmed/6.0 This issue is confirmed to be reproduced on 6.0 at the time this label was set
Projects
Milestone

Comments

@OlafvdSpek
Copy link

OlafvdSpek commented Jun 8, 2018

MariaDB 10.2 supports expressions for field defaults. However pMA escapes and quotes the expression, causing it to become an ordinary string.

ALTER TABLE `photo_likes` CHANGE `created` `created` INT(11) NOT NULL DEFAULT unix_timestamp();

https://mariadb.com/kb/en/library/create-table/#default

@williamdes williamdes added the Bug A problem or regression with an existing feature label Oct 28, 2018
@williamdes
Copy link
Member

williamdes commented Oct 28, 2018

Confirmed.

Have a look to #15006, #14986 for data examples

CREATE TABLE issue_default_values (
  logtime01 varchar(40) NOT NULL DEFAULT current_timestamp,
  logtime0 datetime(2) NOT NULL DEFAULT current_timestamp,
  logtime datetime(2) NOT NULL DEFAULT current_timestamp(2),
  logtext varchar(255) NOT NULL DEFAULT current_timestamp(3),
  logtext11 text NOT NULL DEFAULT current_timestamp(3),
  uuid1 BINARY(16) DEFAULT unhex(replace(uuid(),'-','')),
  uuid int(11) DEFAULT year(now() )
);

@williamdes williamdes added this to To be sorted in issues May 2, 2019
@williamdes williamdes moved this from To be sorted to Reproduced in issues May 4, 2019
@williamdes williamdes moved this from Reproduced to Medium priority in issues Dec 7, 2019
@williamdes williamdes changed the title Support expressions for field defaults Support expressions (functions) for field defaults (table definition), ALTER, INSERT, CREATE? Dec 7, 2019
@OlafvdSpek
Copy link
Author

Could this be fixed before 5.0?

@williamdes
Copy link
Member

@OlafvdSpek If someone finds a fix (before end of this month) that supports all use cases, why not

cc @Tithugues @saurass @ShailuJain @kartik1000

@OlafvdSpek
Copy link
Author

OlafvdSpek commented Dec 10, 2019

Why not block the release until it's fixed?

@williamdes
Copy link
Member

@OlafvdSpek you can email @ibennetch or discuss it here or on #15607 🤷‍♂️
For now the releases are scheduled to the end of the month

@OlafvdSpek
Copy link
Author

OlafvdSpek commented Dec 10, 2019

Ah, you mean whether to block or not.

@yashrajbothra
Copy link
Contributor

I will try to reproduce and fix this 👍

@saurass
Copy link
Contributor

saurass commented Feb 4, 2020

I will try to reproduce and fix this

Yeah sure go ahead : )

@yashrajbothra
Copy link
Contributor

image

Can anyone help me reproduce this I tried it in -> DEMO
using MariaDB

@williamdes
Copy link
Member

@yashrajbothra you need to use difficult examples
See my comment #14371 (comment)

@yashrajbothra

This comment has been minimized.

@williamdes
Copy link
Member

@yashrajbothra did you try queries from the linked issues in my comment?

@yashrajbothra
Copy link
Contributor

Thanks @williamdes So,I looked into all the related issue and What i understood is that the when we insert any pre-defined functions of mysql into value it treats function as string.Execpt current_timestap() .
Because of this ->
image

So, current_timestap(2) is also treated as string.

Did i get it Alright ?

@williamdes
Copy link
Member

williamdes commented Feb 5, 2020

Yes you dit get it alright @yashrajbothra
So I did see PRs that tried to list all possible functions and it was not the right solution because we can use user defined ones and even chained functions etc..

@OlafvdSpek
Copy link
Author

Who approved that condition? ;)

@williamdes
Copy link
Member

@OlafvdSpek can you be more explicit?

@OlafvdSpek
Copy link
Author

The first part is too hard to understand, ($type != 'datetime' && $type != 'timestamp') would be easier.
(Just a comment in general)

@yashrajbothra
Copy link
Contributor

@OlafvdSpek I think this de250fb commit .

@yashrajbothra
Copy link
Contributor

yashrajbothra commented Feb 5, 2020

image

We Expect these USER DEFINED VALUES in Functions right?
Like:
image

@williamdes
Copy link
Member

That would maybe be a solution but be sure that very custom non standard functions also work

Basically I think we can say that the rule is : keep the user defined default value (function or mysql magic constants like timestamp) usable for insertion but also allow that the user changes the function or uses a hard coded value

Thoes conditions make a fix very complicated IMO but a great idea could solve all the cases :)

@yashrajbothra
Copy link
Contributor

yashrajbothra commented Feb 5, 2020

Basically I think we can say that the rule is : keep the user defined default value (function or mysql magic constants like timestamp) usable for insertion but also allow that the user changes the function or uses a hard coded value

But if we keep functions in USER DEFINED DEFAULT VALUES how will we diffrentiate that the input is a function or string?

@williamdes Did you mean to hard code all the mysql function like we did with current_timestamp() ?

@williamdes
Copy link
Member

But if we keep functions in USER DEFINED DEFAULT VALUES how will we diffrentiate that the input is a function or string?

I have no idea it is a challenge to solve :)

And no for hard coding the functions, I will not work because the possibilities are endless

Maybe we could add an option the the selected function that would equal to the default value of the column
But it would be confusing for the user

@williamdes
Copy link
Member

Did you mean if user gives a default function with param

No parameter is possible for a column that defines a function as default value ;)

@yashrajbothra
Copy link
Contributor

Like if CURDATE() doesnt expect a param. we should disable the value for the same. If i am correct than i will definetly include that 👍

@williamdes
Copy link
Member

Like if CURDATE() doesnt expect a param. we should disable the value for the same. If i am correct than i will definetly include that

I think I did not explain well my point

The list will contain

  • functions as it did before
  • a new entry that will be the value of the default value for the column

Obviously it is impossible that it can have a parameter because the table schema will not allow that.

So for that particular option I do not want that the user fills a value that will not be used anyway

Just to say in another way:
When you create the table you define the default value for a column and if you use a function it is impossible to include a parameter.

@yashrajbothra
Copy link
Contributor

I this was able to understand your point 👍

The list will contain

  • functions as it did before
  • a new entry that will be the value of the default value for the column

but If in case lets say i have a table with a column without any default value. When i insert data from INSERT form i want to use USER_DEFINED function (which is not a default value). Can we do that as of now?.

Or this is totally diffrent issue?

@williamdes
Copy link
Member

williamdes commented Feb 15, 2020

I this was able to understand your point

The list will contain

  • functions as it did before
  • a new entry that will be the value of the default value for the column

but If in case lets say i have a table with a column without any default value. When i insert data from INSERT form i want to use USER_DEFINED function (which is not a default value). Can we do that as of now?.

Or this is totally diffrent issue?

;)

Or this is totally diffrent issue?

Yes, I would like to avoid mixing features :)

@yashrajbothra yashrajbothra linked a pull request Feb 17, 2020 that will close this issue
@ghost
Copy link

ghost commented Jul 28, 2020

2 years later and still not fixed...

@saurass
Copy link
Contributor

saurass commented Jul 28, 2020

2 years later and still not fixed...

Hello, @zaseth we are working on this issue, Please refer to this PR #15975

@williamdes williamdes added this to the 5.1.0 milestone Jul 28, 2020
@ChrisHSandN
Copy link

Reading thought this thread, I wonder if maybe an easier solution is to add the DEFAULT() function as an <option> on the insert page to the function <select> dropdown. Even if nothing else was done this would at least allow users to pick that option manually as a workaround for #15006 etc.

Ideally this <option> could act a bit like the Null checkbox, where it is automatically selected (when there is a default entry for that field) but is cleared as soon as you start typing a value into the <textarea>.

The insert SQL with the option selected could then be e.g. INSERT INTO t (i,j) VALUES (DEFAULT,'foobar'); which would avoid all the complexities of having to parse the actual function expression and support parameters etc.


For the alter CREATE/ALTER views would it be too naïve just to look for a leading ' or " in the information_schema.COLUMNS.COLUMN_DEFAULT field, If it has one it is a string value, if not it is a function?

That could be combined with a simple Is Function checkbox which turns off the automatic quoting/escaping when checked.

@williamdes
Copy link
Member

Thank you for the feedback, adding the DEFAULT function seems a good idea !

And adding a Is Function also seems a very good idea !

@OlafvdSpek
Copy link
Author

Reading thought this thread, I wonder if maybe an easier solution is to add the DEFAULT() function as an <option> on the insert page to the function <select> dropdown. Even if nothing else was done this would at least allow users to pick that option manually as a workaround for #15006 etc.

Ideally this <option> could act a bit like the Null checkbox, where it is automatically selected (when there is a default entry for that field) but is cleared as soon as you start typing a value into the <textarea>.

Isn't mapping empty to default enough for the insert form?

@ChrisHSandN
Copy link

Isn't mapping empty to default enough for the insert form?

If the empty function <option> = DEFAULT then there would be no way to override that and insert a '' (blank string) as the value.

DEFAULT(): If the column has no default value ... an error is returned.
https://mariadb.com/kb/en/default/

For fields that don't have a DEFAULT value set you would have to treat these differently or DEFAULT() would produce an error if used in the INSERT SQL.

@OlafvdSpek
Copy link
Author

For fields that don't have a DEFAULT value set you would have to treat these differently or DEFAULT() would produce an error if used in the INSERT SQL.

Currently '' is used for 'empty' integers, which produces a warning.
Fields without a default value need a value anyway..

@ibennetch ibennetch modified the milestones: 5.1.0, 5.1.1 Feb 24, 2021
@williamdes williamdes modified the milestones: 5.1.1, 5.2.0 Apr 22, 2021
@alexander-zierhut
Copy link

+1 Very glad seeing this in the 5.2.0 milestone! Thank you

@ibennetch ibennetch modified the milestones: 5.2.0, 5.2.1 May 11, 2022
@williamdes williamdes added the affects/5.2 This issue or pull-request affects 5.2.x releases (and maybe further versions) label Oct 25, 2022
@williamdes williamdes removed this from the 5.2.1 milestone Oct 25, 2022
@williamdes williamdes added affects/6.0 This issue or pull-request affects 6.0.x releases (and maybe further versions) confirmed/5.2 This issue is confirmed to be reproduced on 5.2 at the time this label was set confirmed/6.0 This issue is confirmed to be reproduced on 6.0 at the time this label was set labels Oct 25, 2022
@williamdes williamdes added this to the 5.3.0 milestone Oct 26, 2022
@Nefcanto
Copy link

This is still not solved. I use uuid() as the default value for some of my string keys in my CMS. For example, the Slug or Key fields are either valid strings, or if they are not filled, instead of creating some random ugly string, I have decided to fill them with uuid values.

Thus they are string columns, that have uuid() set as their default values.

When I change the order of columns via phpMyAdmin, they would be wrapped with single, or multiple quotes, and the uuid() becomes a simple string like 'uuid()' or '''uuid()'''.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects/5.2 This issue or pull-request affects 5.2.x releases (and maybe further versions) affects/6.0 This issue or pull-request affects 6.0.x releases (and maybe further versions) Bug A problem or regression with an existing feature confirmed/5.2 This issue is confirmed to be reproduced on 5.2 at the time this label was set confirmed/6.0 This issue is confirmed to be reproduced on 6.0 at the time this label was set
Projects
issues
  
Medium priority
Development

Successfully merging a pull request may close this issue.

8 participants