Skip to content

Bug Report: Incorrect field assignment in RETURNING clause #415

@Uwanggood

Description

@Uwanggood

Bug Report: Incorrect field assignment in RETURNING clause

Description

When using xo to generate Go code for a PostgreSQL table with an identity column, the Insert function incorrectly assigns the returned value to the wrong field.

Version

xo version:  xo 0.0.0-dev

Table Structure

create table fit_sales_dtl
(
    sales_sn              bigint    not null
        references fit_sales,
    sales_dtl_sn          bigint generated always as identity,
    payment_at            timestamp not null,
    card_pay_amount       integer,
    bank_pay_amount       integer,
    cash_pay_amount       integer,
    point_pay_amount      integer,
    is_cash_receipt       boolean,
    payment_plan_cd       varchar(4),
    card_cmpny_cd         varchar(4),
    card_payment_duration integer,
    created_at            timestamp,
    created_user_id       integer,
    updated_at            timestamp,
    updated_user_id       integer,
    deleted_at            timestamp,
    primary key (sales_sn, sales_dtl_sn)
);

Generated Code

func (fsd *FitSalesDtl) Insert(ctx context.Context, db DB) error {
    // ... (omitted for brevity)
    const sqlstr = `INSERT INTO erp.fit_sales_dtl (
        sales_sn, payment_at, card_pay_amount, bank_pay_amount, cash_pay_amount, point_pay_amount, is_cash_receipt, payment_plan_cd, card_cmpny_cd, card_payment_duration, created_at, created_user_id, updated_at, updated_user_id, deleted_at
    ) VALUES (
        $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15
    ) RETURNING sales_dtl_sn`
    // run
    if err := db.QueryRowContext(ctx, sqlstr, fsd.SalesSn, fsd.PaymentAt, fsd.CardPayAmount, fsd.BankPayAmount, fsd.CashPayAmount, fsd.PointPayAmount, fsd.IsCashReceipt, fsd.PaymentPlanCd, fsd.CardCmpnyCd, fsd.CardPaymentDuration, fsd.CreatedAt, fsd.CreatedUserID, fsd.UpdatedAt, fsd.UpdatedUserID, fsd.DeletedAt).Scan(&fsd.SalesSn); err != nil {
        return logerror(err)
    }
    // ... (omitted for brevity)
}

Expected Behavior

The RETURNING sales_dtl_sn should be scanned into &fsd.SalesDtlSn instead of &fsd.SalesSn.

Actual Behavior

The generated code incorrectly scans the returned sales_dtl_sn into &fsd.SalesSn.

xo Command

xo schema \
  "postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME}?sslmode=disable&search_path=${SCHEMA},public" \
  -s "$SCHEMA" \
  -o "$OUTPUT_DIR" \
  --go-pkg models \
  --go-context only \
  --go-field-tag "db:\"{{ .SQLName }}\" json:\"{{ .SQLName }}\"" \
  -v

Additional Notes

Manually changing &fsd.SalesSn to &fsd.SalesDtlSn in the generated code fixes the issue, but this change is lost when regenerating the code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions