Replies: 3 comments 2 replies
-
I think I've found the difference between creating FKs pointing to another table UK and to "self" UK.
class Origin(Table, tablename="origin"):
id = Serial(primary_key=True)
uk_id = Varchar(length=255, null=False, unique=True)
class Test(Table, tablename="test"):
id = Serial(primary_key=True)
child_id = ForeignKey(Origin, null=True, index=True, target_column="uk_id") The migration file includes, before the
class Test(Table, tablename="test"):
id = Serial(primary_key=True)
uk_id = Varchar(length=255, null=False, unique=True)
child_id = ForeignKey("self", null=True, index=True, target_column="uk_id") Then the migration file is missing that section defining the "self" ( So, I've edited the migration file manually, adding a class Test(Table, tablename="test", schema=None): # <== This is the missing declaration in the "self" case.
uk_id = Varchar(
length=255,
default=None,
null=False,
unique=True,
...
...
)
...
...
async def forwards():
...
...
manager.add_column(
table_class_name="Test",
tablename="test",
column_name="child_id",
db_column_name="child_id",
column_class_name="ForeignKey",
column_class=ForeignKey,
params={
"references": Test, # <== had to change this too, from "self" (string) to Test (the class).
"on_delete": OnDelete.no_action,
"on_update": OnUpdate.no_action,
"target_column": "uk_id",
....
},
schema=None,
) So, should this be considered a bug and that section defining the involved target columns should be added in the "self" case? |
Beta Was this translation helpful? Give feedback.
-
@stronk7 I cannot reproduce your issue. In my case everything works fine. With your table definition
I can create and run the migration. |
Beta Was this translation helpful? Give feedback.
-
Well, when the FK points to a remote (another table) UK, then it works perfectly and the information from the I'd expect the FK pointing to a self (same table) UK to behave the same, and lead to working migrations. Cannot see why it makes any difference and they just fail because of the lack of that information. I'm not sure it's an edge case, for me it's exactly the same than the "remote" UK one. In any case, yes, I can workaround it by adding the preamble information by hand, or by importing the table, although... the 2nd could be not future-proof if at some time some of the columns go away (I think that's the reason the migrations always write "current" correct preambles instead of importing, because the later are not good snapshots. Ciao :-) |
Beta Was this translation helpful? Give feedback.
-
This is a followup of #393.
I've been using FKs pointing to other tables UKs perfectly. But now, I've the typical parent-child relationship that uses a "self" UK. Sort of:
The definition above leads to the creation of an apparently good migration file, but when I run migrations, it fails with "piccolo/query/base.py", line 387, in postgres_ddl
raise NotImplementedError".
I also have tried "self.uk_id" (string), uk_id (property name), ... but in all those cases the creation of migration file doesn't work. Also have looked to some example in the docs and issues about how to make a FK to point to a "self" UK column (PK ones work ok, and also "remote" UK ones work ok) with no luck.
So, is there any "trick" to get those "self" + "uk" ones working? TIA!
Beta Was this translation helpful? Give feedback.
All reactions