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

[MySQL] detect or exclude GENERATED ALWAYS VIRTUAL columns from mutable columns #454

Open
mutefiRe opened this issue Feb 19, 2025 · 1 comment
Labels

Comments

@mutefiRe
Copy link

mutefiRe commented Feb 19, 2025

Related Issues

#209
#300

This seems to be solved for PostgreSQL already, so opening another issue for MySQL.

Is your feature request related to a problem? Please describe.

If having a MySQL table for e.g. cars that supports soft deletion via a generated column. The schema could look like this:

CREATE TABLE car (
  car_id int NOT NULL AUTO_INCREMENT,
  license_plate VARCHAR(10) NOT NULL,
  deleted_at TIMESTAMP NULL,
  not_deleted INT(1) GENERATED ALWAYS AS (IF(`deleted_at` IS NULL, 1, NULL)) VIRTUAL,
  CONSTRAINT `car_unique` UNIQUE (license_plate, not_deleted),
  PRIMARY KEY (car_id)
);

With this setup, an entry is “soft-deleted” by setting deleted_at. Additionally when using not_deleted as an GENERATED ALWAYS column, it should probably not be part of insert or update statements. However insert with MutableColumns includes the virtual column, causing an error since MySQL does not allow direct insertion into a GENERATED ALWAYS column.

Background: The nullable generated column helps with the unique constraint over non-deleted rows.

Describe the solution you'd like

Properly detect and exclude GENERATED ALWAYS columns from the mutable columns so that the schema above can be used without causing SQL errors. This would allow GENERATED ALWAYS columns to work as intended, without manual workarounds in or around the generated code, which excludes the column manually.

@go-jet
Copy link
Owner

go-jet commented Feb 20, 2025

Agree, make sense.

@go-jet go-jet added the good first issue Good for newcomers label Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants