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

incorrect CTE generation #986

Open
jurakontor opened this issue May 17, 2024 · 0 comments
Open

incorrect CTE generation #986

jurakontor opened this issue May 17, 2024 · 0 comments

Comments

@jurakontor
Copy link

hello
it looks like CTE (.With) doesn't generate proper sql query

I have following sql:

WITH ins
AS ( INSERT INTO profile_source_on_chain (address, nick_name) VALUES ('0x72c9fb7ed19d3ce51cea5c56b3e023cd918baadf3', 'Sei Ape Club') ON CONFLICT (address, nick_name) DO NOTHING RETURNING id)
SELECT id
FROM ins
UNION ALL
SELECT id
FROM profile_source_on_chain
WHERE address = '0x72c9fb7ed19d3ce51cea5c56b3e023cd918baadf3'
and nick_name = 'Sei Ape Club';
`

and i need to use bun to create same sql.

ProfileSourceOnChain is table with 3 columns: id (uuid), address , nickname varchar
Have constraint (address , nickname)

`
onChain := ProfileSourceOnChain{Address: "address",NickName:"nickname"}

	subq := tx.NewInsert().Model(&onChain).Returning("id").
		On("CONFLICT (address, nick_name) DO nothing")
	
	union := tx.NewSelect().Model((*dto2.ProfileSourceOnChain)(nil)).Column("id").
		Where("address = ?","address").
		Where("nick_name = ?","nickname")

        tx.NewSelect().UnionAll(union).Column("id").Table("ins").With("ins", subq).Scan(ctx, &r); `

the above code will generate

(WITH ins
AS ( INSERT INTO profile_source_on_chain (address, nick_name) VALUES ('0x72c9fb7ed19d3ce51cea5c56b3e023cd918baadf3', 'Sei Ape Club') ON CONFLICT (address, nick_name) DO NOTHING RETURNING id)
SELECT id
FROM ins)
UNION ALL
SELECT id
FROM profile_source_on_chain
WHERE address = '0x72c9fb7ed19d3ce51cea5c56b3e023cd918baadf3'
and nick_name = 'Sei Ape Club';

the issue with brackets at the beginning and after FROM ins.
Execution of the sql gives an error:

ERROR: WITH clause containing a data-modifying statement must be at the top level

Need help with fixing this issue. thanks

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

1 participant