Skip to content

Instantly share code, notes, and snippets.

@gcko
Last active August 7, 2023 09:32
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save gcko/de1383080e9f8fb7d208 to your computer and use it in GitHub Desktop.
Save gcko/de1383080e9f8fb7d208 to your computer and use it in GitHub Desktop.
Django Custom Model ForeignKey Field for Spanning Databases
@dima-kov
Copy link

@ldsad7, as for us, we refused this feature and replaced it with regular microservices.

@sijianlin
Copy link

Hello. The code has been working well but today I got unexpected error of 'SpanningForeignKey' object has no attribute 'rel'. I looked at the ForeignKey code and it seems like the "rel" attribute has been changed to "remote_field"? And res.to seems to be changed to remote_field.model? Do you need to update the code?
Thanks, Sijian

@joshua2000
Copy link

joshua2000 commented Apr 27, 2020

**If you are using mysql and django2.2, kindly use the following for the ForeignKey **

**PROBE THANKS TO Jared Scott aka gcko **

from django.core import exceptions
from django.db.models.fields.related import ForeignKey
from django.db.utils import ConnectionHandler, ConnectionRouter
#from django.db.models import ForeignKey
connection = ConnectionHandler()
router = ConnectionRouter()

class  SpanningForeignKey(ForeignKey):
    def validate(self, value, model_instance):
        if self.remote_field.parent_link:
            return
        # Call the grandparent rather than the parent to skip validation
        super(ForeignKey, self).validate(value, model_instance)
        
        if value is None:
            return

        using = router.db_for_read(self.remote_field.model, instance=model_instance)
        qs = self.remote_field.model._default_manager.using(using).filter(
            **{self.remote_field.field_name: value}
        )

        qs = qs.complex_filter(self.get_limit_choices_to())
        if not qs.exists():
            raise exceptions.ValidationError(
                self.error_messages['invalid'],
                code='invalid',
                params={
                    'model': self.remote_field.model._meta.verbose_name, 
                    'pk': value,
                    'field': self.remote_field.field_name, 'value': value,
                },  # 'pk' is included for backwards compatibility
            )

@Sarrus1
Copy link

Sarrus1 commented Jul 17, 2020

Hello,

Thanks you very much for your solution! However, it seems like migrations are not registering the SpanningForeignKey field. Adding them manually does not fix the issue aswell. Do you have any idea what is going wrong?

Regards,
Charles

@joshua2000
Copy link

@Sarrus1, whats the problem

@diegobill
Copy link

is there any solution for manytomanyfield?

@joshua2000
Copy link

@Sarrus1, cut and paste the error for further help..@diegobill, have not seen one,but keep on looking, the internet is a strange place

@pnoziska
Copy link

I'm trying to use this class to allow for a lookup of a user in auth_user in a 'default' DB , different from my application DB. ('test2' here).
the table I created looks like:

CREATE TABLE IF NOT EXISTS "test2_m1" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "description" text NOT NULL, "user_id" integer NOT NULL ); -- no foreign key

But when I run these statements, Django generates the following query wth an INNER JOIN to auth_user, and SpanningForeignKey.validate() is not called at all (I added a debug line to test):

from test2.models import *
M1.objects.get(user__username = 'xyz')
db_for_read: test2 ---> test2
ZZZZ SELECT "test2_m1"."id", "test2_m1"."user_id", "test2_m1"."description" FROM "test2_m1" INNER JOIN "auth_user" ON ("test2_m1"."user_id" = "auth_user"."id") WHERE "auth_user"."username" = xyz

The problem here is that the underlying Django code still wants to form a query with an INNER JOIN on the app-specific DB, not the default DB, expecting "auth_user" to be in that DB.

My questions:

  1. How do I get Django to NOT do that INNER JOIN on "auth_user" , especially when I specified no foreign key in the app's DB ? Shouldn't the SpanningForeignKey class prevent this from happening ?
  2. If SpanningForeignKey.validate() won't do that for me, what other method(s) must I override ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment