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

Support many-to-many via array column instead of via a join table #1868

Closed
aeb-dev opened this issue May 28, 2021 · 10 comments
Closed

Support many-to-many via array column instead of via a join table #1868

aeb-dev opened this issue May 28, 2021 · 10 comments
Labels
duplicate This issue or pull request already exists

Comments

@aeb-dev
Copy link

aeb-dev commented May 28, 2021

I am trying to generate the following sql with linq but no success so far:

SELECT
    t1.id,
    t2.id
FROM public.table1 t1
INNER JOIN public.table2 t2 ON ARRAY[t2.id]::bigint[] <@ t1.t2_ids;

Table1:

public class Table1
{
    public long Id { get;set; }
    public long[] T2Ids { get; set }
}

Table2:

public class Table2
{
    public long Id { get;set; }
}

Is this possible with LINQ?

@roji
Copy link
Member

roji commented May 28, 2021

Try this:

from t1 in ctx.Table1
from t2 in ctx.Table2
where t1.T2Ids.Contains(t1.Id)
select t1

@aeb-dev
Copy link
Author

aeb-dev commented May 28, 2021

It produces the following

SELECT t1.id, t1.t2_ids
FROM table1 AS t1
CROSS JOIN table2 AS t2
WHERE t1.t2_ids @> ARRAY[t1.id]::bigint[]

Also I meant linq with methods sry :)

I might have oversimplified the example:
There are no FK's between the tables because Table1 is partitioned on another field.

To replace the effect of FKs, I simply added the following code with respective fields

t1Entity.HasMany(d => d.Table2s)
            .WithMany(p => p.Table1s);

t2Entity.HasMany(d => d.Table1s)
            .WithMany(p => p.Table2s);

@roji
Copy link
Member

roji commented May 28, 2021

@Mrmumu you're apparently trying to mix EF Core's many-to-many with array columns in some complicated way - that's not likely to work. EF Core manages many-to-many by having a third join table in the middle, with foreign keys to each side. If you need further help, please post a full, runnable code sample with the complete model and query you're trying to do.

@aeb-dev
Copy link
Author

aeb-dev commented May 31, 2021

I will try to prepare an example

@aeb-dev
Copy link
Author

aeb-dev commented Jun 2, 2021

Sorry for the late response

check this repo: https://github.com/MrMuMu/ef-postgres-array-join

Now the question is how do I generate the following query on linq method syntax:

SELECT
    t1.id,
    t2.id,
    t2.name
FROM public.table1 t1
INNER JOIN public.table2 t2 ON ARRAY[t2.id]::bigint[] <@ t1.table2_ids;

update: new field on table2

@roji roji changed the title Linq to Sql generation for inner joins on array columns Support many-to-many via array column instead of via a join table Dec 29, 2021
@roji roji added the enhancement New feature or request label Dec 29, 2021
@roji roji added this to the Backlog milestone Dec 29, 2021
@roji
Copy link
Member

roji commented Dec 29, 2021

This is definitely not something that EF Core can generate at this point.

Note dotnet/efcore#23523 which is about providing this for Cosmos (some info also in dotnet/efcore#16920 (comment)); the same work could be done for PostgreSQL as well. Keeping on the backlog.

@roji
Copy link
Member

roji commented Jun 14, 2022

@Bouke that sentence simply talks about storing arbitrary data values (numbers, strings) and not for having EF Core automatically manage many-to-many via PG arrays (i.e. store foreign keys in the array).

@Bouke
Copy link

Bouke commented Jun 15, 2022

@roji I missed your reply (normally this view auto-updates) and have deleted my original comment as re-reading the comment indeed didn't suggest this exact use-case. Thank you for confirming the same.

@roji
Copy link
Member

roji commented Apr 22, 2023

With the work on primitive collections in EF Core (dotnet/efcore#30731), this would be a general EF feature - in other databases which don't support arrays, a JSON array could hold the FKs of the related entities.

@roji
Copy link
Member

roji commented Apr 22, 2023

Duplicate of dotnet/efcore#30551

@roji roji marked this as a duplicate of dotnet/efcore#30551 Apr 22, 2023
@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Apr 22, 2023
@roji roji removed this from the Backlog milestone Apr 22, 2023
@roji roji added duplicate This issue or pull request already exists and removed enhancement New feature or request labels Apr 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants