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

fix: table not change (#6913) #7266

Closed
wants to merge 1 commit into from

Conversation

ZiRunHua
Copy link

@ZiRunHua ZiRunHua commented Nov 5, 2024

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

Update Condition for Table Selection: This change allows ParseWithSpecialTableName to re-evaluate and update the table name based on the value passed, not just when the table name is initially empty.

User Case Description

When you use embedded structures, you might write queries like this

type StatisticBase struct {
    Date          time.Time
    Amount, Count int
}

type IncomeStatistic struct {
    StatisticBase
    Tas    int
    UserId uint
}

type ExpendStatistic struct {
    StatisticBase
    UserId uint
}

type Total struct{ Amount, Count int }

type Statistic struct{ Income, Expend Total }

func queryStatistic(db *gorm.DB, start, end time.Time) (result Statistic, err error) {
    query := db.Where("date BETWEEN ? AND ?", start, end)
    query = query.Select("SUM(amount) as Amount,SUM(count) as Count")
    err = query.Model(&IncomeStatistic{}).Scan(&result.Income).Error
    if err != nil {
        return
    }
    err = query.Model(&ExpendStatistic{}).Scan(&result.Expend).Error
    return
}

But the table for the second query did not change.

SELECT SUM(amount) as Amount,SUM(count) as Count FROM `income_statistic` WHERE date BETWEEN '2024-11-03 09:38:53.615' AND '2024-11-08 09:38:53.615';
SELECT SUM(amount) as Amount,SUM(count) as Count FROM `income_statistic` WHERE date BETWEEN '2024-11-03 09:38:53.615' AND '2024-11-08 09:38:53.615';

Since the table is only updated when empty, this is what the "pull request" wants to change, which will give the user more flexibility to manipulate the data as needed.

@ZiRunHua
Copy link
Author

ZiRunHua commented Nov 5, 2024

Oh, the test didn't pass. Maybe it shouldn't be used that way.

@ZiRunHua ZiRunHua closed this Nov 5, 2024
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.

1 participant