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

Many to many relationship #46

Closed
Efor18 opened this issue Mar 7, 2013 · 9 comments
Closed

Many to many relationship #46

Efor18 opened this issue Mar 7, 2013 · 9 comments

Comments

@Efor18
Copy link

Efor18 commented Mar 7, 2013

Hi,

How could I create a many to many relationship?

@Efor18 Efor18 closed this as completed Mar 7, 2013
@Efor18 Efor18 reopened this Mar 7, 2013
@josh-burton
Copy link

I'm also wondering how to do this.

@vonloxley
Copy link
Contributor

Did you try:

public class Foo extends Model {
[…]
}
public class Bar extends Model {
[…]
}

public class FooBar extends Model {
    @Column(name = "foo", onDelete=ForeignKeyAction.CASCADE)
    public Foo foo;
    @Column(name = "bar" onDelete=ForeignKeyAction.CASCADE)
    public Bar bar;

    public List<Foo> foos() {
        return getMany(Foo.class, "FooBar");
    }
    public List<Bar> bars() {
        return getMany(Bar.class, "FooBar");
    }
}

This will need my branch: https://github.com/vonloxley/ActiveAndroid, because of #32 and is completely untested.

You need to define a unique index on FooBar(foo, bar) as well, which isn’t possible with AA right now.

HTH

@Efor18
Copy link
Author

Efor18 commented Mar 19, 2013

Thanks, finally I did it that way.

@pardom-zz
Copy link
Owner

@vonloxley has it right. This is common db architecture for many-to-many relationships. Rails ActiveRecord handles it a bit differently, whereas this is a bit more transparent.

@alexvbush
Copy link

So this is the way to do many-to-many with ActiveAndroid, through a join Model/Table?

@hayksaakian
Copy link

would be nice if there was some analogue to has_and_belongs_to_many

@SwampMobile
Copy link

How does one define a "unique index on FooBar" as mentioned above?

@waclock
Copy link

waclock commented Oct 5, 2014

Could you give us a more detailed example? I still can't manage to get a many-to-many relationship working. Do I have to explicitly create the "FooBars" when creating a Foo/Bar ?

@prilaga
Copy link

prilaga commented Aug 29, 2016

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

9 participants