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

Setting default value of field, when updating field with NULL value [CORE1311] #1730

Closed
firebird-automations opened this issue Jun 9, 2007 · 13 comments

Comments

@firebird-automations
Copy link
Collaborator

Submitted by: Dejan Schild (schild)

Block progress on CORE3073
Is replaced by CORE5449

Votes: 2

A mandatory field that has been assigned a default value, when updating it with NULL, it's set to NULL, instead of acquiring a preset default value.

For example:
CREATE TABLE KALKULACIJA (
KALK_UID BIGINT NOT NULL,
K_CARINA DOUBLE PRECISION DEFAULT 0 NOT NULL
);

The following command:
INSERT INTO KALKULACIJA (KALK_UID, K_CARINA)
VALUES (1, NULL);

will raise an error:
The insert failed because a column definition includes validation constraints.
validation error for column K_CARINA, value "*** null ***".

It should write following values into the table: KALK_UID=1 and K_CARINA=0 (it's default value).

Reason: When using updatable view, then we must update all fields in table, not just fields that are to be updated. In this situation default values for fields are unusable, and we have to write triggers that will check if fields are NULL. That is double work.

====== Test Details ======

See test for CORE5449

@firebird-automations
Copy link
Collaborator Author

Commented by: @pcisar

AFAIK this is correct according to SQL standard. Default value is assigned only when no value is provided at all (explicit NULL is a value).

@firebird-automations
Copy link
Collaborator Author

Commented by: Dejan Schild (schild)

It's ok.
But, is it possible to make some solution for this problem in updatable view?
I guess that many people use updatable view for synchronize update on few depended tables, and this rule is limitation in that case.

Maybe to add some directive to INSERT/UPDATE command, like this:

CREATE VIEW VE_KALKROBE(
KALK_UID,
K_CARINA)
AS
select
k.KALK_UID,
k.K_CARINA
from KALKULACIJA k;

CREATE trigger ve_kalkrobe_bi5 for ve_kalkrobe
active before insert position 5
AS
begin
INSERT INTO KALKULACIJA (
KALK_UID,
K_CARINA)
VALUES (
NEW.KALK_UID,
NEW.K_CARINA)
WITH DEFVALUESIFNULL; -- or something similar to tell command to set DEFAULT VALUE to field if provided value is NULL! Also use this directive for UPDATE!
end

CREATE trigger ve_kalkrobe_bu5 for ve_kalkrobe
active before update position 5
AS
begin
UPDATE KALKULACIJA
SET
KALK_UID=NEW.KALK_UID,
K_CARINA=NEW.K_CARINA
WHERE KALK_UID=OLD.KALK_UID
WITH DEFVALUESIFNULL; -- or something similar to tell command to set DEFAULT VALUE to field if provided value is NULL!
end

Naturally, use this only for NOT NULL fields who has default value.

@firebird-automations
Copy link
Collaborator Author

Modified by: @pcisar

Workflow: jira [ 12285 ] => Firebird [ 14787 ]

@firebird-automations
Copy link
Collaborator Author

Commented by: Cosmin Apreutesei (cosmin_ap2)

This bit me too, default value are pretty useless in conjunction with updatable views. What's a standard good for if it cannot make its own specified features work together?

AFAIK MSSQL has the keyword default which is a better workaround to the problem, ie. you can say:

insert into t (c1, c2) values (coalesce(new.c1, default), new.c2)

@firebird-automations
Copy link
Collaborator Author

Modified by: @dyemanov

assignee: Dmitry Yemanov [ dimitr ]

@firebird-automations
Copy link
Collaborator Author

Commented by: @asfernandes

Dmitry, are you going to implement UPDATE ... SET field = DEFAULT or INSERT ... VALUES (x, y, DEFAULT)?

If yes, this seems as same blr verb needed to support ALTER ... ADD ... NOT NULL.

@firebird-automations
Copy link
Collaborator Author

Commented by: @dyemanov

I had such intention, but not in the nearest future. Feel free to jump in and intercept this task if you wish ;-)

@firebird-automations
Copy link
Collaborator Author

Modified by: @asfernandes

Link: This issue block progress on CORE3073 [ CORE3073 ]

@firebird-automations
Copy link
Collaborator Author

Commented by: @mrotteveel

The current request is not in line with the SQL standard, the SQL standard way would be to use the context value DEFAULT, which will use the column default (or NULL if there is no column default).

@firebird-automations
Copy link
Collaborator Author

Modified by: @mrotteveel

Link: This issue is replaced by CORE5449 [ CORE5449 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: @asfernandes

status: Open [ 1 ] => Resolved [ 5 ]

resolution: Won't Fix [ 2 ]

assignee: Dmitry Yemanov [ dimitr ] =>

@firebird-automations
Copy link
Collaborator Author

Modified by: @pcisar

status: Resolved [ 5 ] => Closed [ 6 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: @pavel-zotov

status: Closed [ 6 ] => Closed [ 6 ]

QA Status: Covered by another test(s)

Test Details: See test for CORE5449

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

No branches or pull requests

1 participant