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

Can I use transactions with the CRUD code generated by goctl? #4162

Open
vine2024 opened this issue May 24, 2024 · 1 comment
Open

Can I use transactions with the CRUD code generated by goctl? #4162

vine2024 opened this issue May 24, 2024 · 1 comment

Comments

@vine2024
Copy link

vine2024 commented May 24, 2024

go-zero自动生成的crud如下:

	subscriptionsModel interface {
		Insert(ctx context.Context, data *Subscriptions) (sql.Result, error)
		FindOne(ctx context.Context, id int64) (*Subscriptions, error)
		Update(ctx context.Context, data *Subscriptions) error
		Delete(ctx context.Context, id int64) error
	}
	usersModel interface {
		Insert(ctx context.Context, data *Users) (sql.Result, error)
		FindOne(ctx context.Context, id int64) (*Users, error)
		Update(ctx context.Context, data *Users) error
		Delete(ctx context.Context, id int64) error
	}

如果我想使用事务,应该怎么样直接调用生成的crud?
难道我只能重新写一个insert和update方法来操作吗,像这样。有没有更方便的方法?

err = c.Conn.TransactCtx(context.Background(), func(ctx context.Context, session sqlx.Session) error {
		query, _, err := goqu.Dialect("mysql").Insert(c.SubscriptionsModel.GetTableName()).Rows(subscription).ToSQL()
		if err != nil {
			return err
		}
		r, err := session.ExecCtx(ctx, query)
		if err != nil {
			return err
		}
		subscription.Id, _ = r.LastInsertId()

		query, _, err = goqu.Dialect("mysql").Update(c.UsersModel.GetTableName()).
			Set(goqu.Record{"last_subscription_id": subscription.Id}).
			Where(goqu.Ex{"id": user.Id}).
			ToSQL()
		if err != nil {
			return err
		}
		_, err = session.ExecCtx(ctx, query)
		if err != nil {
			return err
		}
		return nil
	})
@kevwan kevwan changed the title 用go-zero生成的crud可以使用事务吗? Can I use transactions with the CRUD code generated by goctl? May 26, 2024
@kesonan
Copy link
Collaborator

kesonan commented May 26, 2024

Not supported yet, and no plan to support it.

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

No branches or pull requests

2 participants