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

Delete statements with joins not supported #16

Closed
ceastwood opened this issue Oct 6, 2017 · 6 comments
Closed

Delete statements with joins not supported #16

ceastwood opened this issue Oct 6, 2017 · 6 comments

Comments

@ceastwood
Copy link
Contributor

ceastwood commented Oct 6, 2017

I made a typo on the alias, sorry about that.

Does not support delete statement joins
I'll try and work on a PR for this if I can finish work sometime this next week. I can see where it goes wrong in the compiler.

Example simple use case; I have a couple more complex ones too:

DELETE foo
FROM [dbo].[Foo] foo
JOIN [dbo].[Bar] bar ON foo.[FooId] = bar.[FooId]
WHERE bar.[IsWorking] = 0

Query as DELETE (broken):

new Query("dbo.Foo as foo") .Join("dbo.Bar as bar", "foo.FooId", "bar.FooId") .Where("bar.IsWorking", 0) .Delete()

Produces

DELETE FROM [dbo].[Foo] AS [foo] WHERE [bar].[IsWorking] = 0

@ceastwood ceastwood changed the title Delete statements with alias and/or joins not supported Delete statements with joins not supported Oct 6, 2017
@ahmad-moussawi
Copy link
Contributor

Thanks for reporting, yes currently these cases are not supported in DELETE, INSERT and UPDATE statements, the main reason is that the join method is not supported in all db engines, for example in Postgres, so don't know what is the best way to handle this, maybe we can:

  1. converting the join to a CTE or subquery (while I don't recommend such implicit conversion)
  2. or throw an exception to the user for the unsupported engines
  3. or compile it and let the user handle these cases
    4 or ignore it at all (current)

What do you think ?

@ahmad-moussawi
Copy link
Contributor

for the moment, I agree that we should mention this in the docs, could you open an issue there please, or I do it myself ?

@ceastwood
Copy link
Contributor Author

I will open the issue there later if I don't see it already.

Yeah, I don't think making the implicit conversion is the way to go--I was just looking up the USING statement in postgres but I'd have to come back to that.

In a local dev branch, I've started to build onto the UPDATE/INSERT for sqlserver compiler as well as add tests. I think since the library already just ignores the unsupported statement it can stay that way until someone finds a reason not to silently ignore it.

@punssoma
Copy link

Any updates on adding join to insert/update/delete. Eagerly waiting for it. Was looking for a solution for SQLServer.

@ahmad-moussawi
Copy link
Contributor

@punssoma
Do you have a proposal on how this can be accomplished in the following database:

  • Sql Server
  • MySql
  • PostgreSql

for the time being I suggest using subqueries something like

// delete the books where with AuthorId = 1
db.Query("Books").WhereIn("Id", q => q.From("Books").Join("Authors").Where("Authors.Id", 1).Select("Id"))

@punssoma
Copy link

@ahmad-moussawi Thanks for the tip.
No proposal as of now. Let me try something locally and then may be i can suggest

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

3 participants