piccolo-orm / piccolo

A fast, user friendly ORM and query builder which supports asyncio.
https://piccolo-orm.com/
MIT License
1.45k stars 91 forks source link

Foreign Keys that reference non pk columns #394

Open dantownsend opened 2 years ago

dantownsend commented 2 years ago

Discussed in https://github.com/piccolo-orm/piccolo/discussions/393

Originally posted by **theelderbeever** January 13, 2022 Is it possible to create a foreign key on a table that references a column other than the primary key on a table? The documentation seems to imply that you can only pass a table to `ForeignKey` and it picks up the pk. I would like to do something like the following... ```python class Contract(BaseTable): address = Varchar(index=True, required=True, null=False, unique=True) class Log(BaseTable): contract_address = ForeignKey(Contract.address) ``` Is there a way to accomplish this?
dantownsend commented 2 years ago

Reopening, as there are still changes required to Piccolo Admin and Piccolo API.

sinisaos commented 2 years ago

@dantownsend Fix for Piccolo Api and Piccolo Admin is here