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

Small Change: Allow calling struct.Exists() without having to pass on PK fields #1202

Merged
merged 1 commit into from
Sep 26, 2022

Conversation

MJacred
Copy link
Contributor

@MJacred MJacred commented Sep 26, 2022

For when having a helper interface that has Insert(), Update() and Upsert().
Upsert() is not always possible, e.g. if table has only one field (the PK); or composite PK (mysql).

An alternative is necessary. Therefore, knowing whether the record exists helps to decide between Insert() and Update().

This enables e.g. generic mass loads from external files.

type DBHelper interface {
	Exists(ctx context.Context, exec boil.ContextExecutor) (bool, error)
	Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error
	Update(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) (int64, error)
	Upsert(ctx context.Context, exec boil.ContextExecutor, updateColumns, insertColumns boil.Columns) error
} 

Workaround for #328, if you e.g. flag upsert-incompatible tables and use interface such as above.

Example

troublesome table in question

CREATE TABLE LanguageText (
    lang_id         CHAR(3) NOT NULL COMMENT 'ISO 639-3 Code', -- "eng"
    localization_id CHAR(3) NOT NULL COMMENT 'ISO 639-3 Code', -- "deu"

    text CHAR(20) NOT NULL,

    PRIMARY KEY (lang_id,localization_id),
    CONSTRAINT languagetext_language_fk_1
        FOREIGN KEY (lang_id) REFERENCES Language (id)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT,
    CONSTRAINT languagetext_language_fk_2
        FOREIGN KEY (localization_id) REFERENCES Language (id)
        ON DELETE RESTRICT
        ON UPDATE RESTRICT
);

result of template execution

// Exists checks if the LanguageText row exists.
func (o *LanguageText) Exists(ctx context.Context, exec boil.ContextExecutor) (bool, error) {
	return LanguageTextExists(ctx, exec, o.LangID, o.LocalizationID)
}

For when having a helper interface that has Insert(), Update(), Upsert().
Upsert() is not always possible. Knowing that the record exists helps to decide between Insert() and Update().
@stephenafamo stephenafamo merged commit f6f4805 into volatiletech:master Sep 26, 2022
@MJacred MJacred deleted the features/exists branch September 27, 2022 08:21
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