-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 On Mon, Feb 11, 2019 at 01:08:41AM -0800, 'Lance Ellinghaus' via Django users wrote: > I have seen references to people adding the support of composite primary > keys to Django for years, but it does not look like it has been added. > > I have a number of legacy databases that I cannot add fields to but need to > be able to be queried through Django. They are read-only tables. > A number of the tables are also related through Composite Foreign Keys and > I have seen some addons but not sure what is fully supported. > > I am trying to use Django 2.1.5. > > Any help or ideas would be appreciated, and saying "Add a field to your > tables" is not an option. > > Thank you.
Hi Lance, I haven't been on top of this in recent years, but let me try. When it comes to just composite primary keys, as long as you only need read access, you might be able to get away with marking an arbitrary field as the primary key. Using a non-unique field as the primary key would make any writes to the database dangerous, but that's not your case here. However, you also lose the invariant that filtering on the “pk” field alias will match at most one row, which means the admin would probably be off the table, too – since that assumes the “pk” is, in fact, unique, and the same holds for any other package that makes use of “pk”. You should still be okay in your own code, though. For composite foreign keys, there is nowadays a private, undocumented API, ForeignObject, which is what ForeignKey builds on. The general ForeignObject lets you specify multiple fields that make up the relation, you should be able to find some examples in the test suite (there is a foreign_object package in there). Keep in mind that this is an internal API, so it might change without warning. Good luck, Michal -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCgAGBQJcYXniAAoJEHA7T/IPM/kl8EAP/Rzaxqe0bDO6vkxb4/Mmmj5S 62t6Cfm3VqqK6NxYjRXG904jKwDI4HMH1SAs5fdVMh9vf+VBj/S5bcIpQbesqXbh wbkcIn+mmnfhLDaHOGunYqp76tscBJv/rtxJZaX5NRLp7OraCWtbjRBtiw1fJ/tl iJD0HRIk9zn0pqyX8GjeAyM0UR+uL1wwrvz8Ur85ASsc8pFThTP6ZMQoaEIgo9D+ HB3XfrhiOMd5Nb2SbjG4KCRe7alpFx83nuY53YVsv+8X+Nqp4Ndi7ch2Ni3jxxHf R7qVJYsMp/l72CNb6KiT85sb6PwQyeVdvU78cXKjkIKDirskGgQIEW2OvK+ZXC3B Aj4I+AMFXEKje8ITfW3/s/v+UNvVTNZHYC5NPZ6o50+YJFDoiwb0mpQMdUredWZk nT2cYMgyPQA/XKN9w5vUCNbTebPG/AA7yCthXbOrHn9Xl+kICkXRjjl9fHRUs55Q wgPqz0CFXPKaE+JFt/NABJzdSrki1y587GHsOb7hKFiQwS8DHU8WOMXkR3BGKEtN q0gebcqKvVnjRwLLsBvB0h3uI/yifgBTp8G8/+tZ0WuNdongTe+yPzchxuRjgx10 e70xtBjgsyCQGRB3CPsgR2UelQOtAEt8ZyYwyPF5sQYOvQg1xjcYp87FqfqflSTE 6aQIMkXhKjKazmc/ew9F =Ji5Z -----END PGP SIGNATURE----- -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20190211133426.GV10973%40koniiiik.org. For more options, visit https://groups.google.com/d/optout.