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

WITH clause without a RETURNING clause will panic #177

Closed
1 of 3 tasks
randallmlough opened this issue Nov 2, 2019 · 0 comments
Closed
1 of 3 tasks

WITH clause without a RETURNING clause will panic #177

randallmlough opened this issue Nov 2, 2019 · 0 comments

Comments

@randallmlough
Copy link

Describe the bug
I'll start with the SQL I'm trying to create/execute

WITH ins1 AS (
INSERT INTO account (
                email,
		status,
		uuid)
		VALUES(
                        'email@email.com',
			'active',
			'XXX-XXX-XXXXX')
	RETURNING
		*
),
ins2 AS (
INSERT INTO account_user (account_id,
		user_id)
SELECT
	id,
	6
FROM
	ins1
)
SELECT
	*
FROM
	ins1

The goal with the above SQL is to insert into one table, insert the returned data to a join table, and finally, return all the columns from the first row/execution.

To Reproduce

goqu.Dialect("postgres").
		From("ins1").
		With("ins1",
			goqu.Dialect("postgres").
				Insert("account").
				Rows(Account{Email:"email@email.com", Status:"active",UUID:"XXX-XXX-XXXX"}).
				Returning("*"),
		).
		With("ins2",
			goqu.Dialect("postgres").
			Insert("account_user").
			Cols("account_id", "user_id").
			FromQuery(goqu.Dialect("postgres").
				From("ins1").
				Select(
					"id",
					goqu.V(req.UserID),
				),
			).
			Returning("user_id"), // will panic if not included even though I don't need the return
		).
		Select("*").ToSQL()

The above works, however, the Returning clause on the second With clause is required even though I don't need to do anything with it. If a Returning clause isn't present within a with clause, goqu will panic. It took a lot of banging my head against the wall trying to figure out why it's panicing since I'm not using any of the data therefore I wasn't including a return clause.

Dialect:

  • postgres
  • mysql
  • sqlite3
doug-martin added a commit that referenced this issue Dec 7, 2019
* Removed check for returning columns when using insert/update/delete in sub select
@doug-martin doug-martin added this to the v9.5.1 milestone Dec 7, 2019
doug-martin added a commit that referenced this issue Dec 7, 2019
[FIXED] WITH clause without a RETURNING clause will panic [#177]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants