-
Notifications
You must be signed in to change notification settings - Fork 91
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
Support composite unique constraints in auto migration #957
Conversation
Thanks a lot for this! I did a quick review, and it looks good. I appreciate the work you've put into it. As it's quite a big feature, I'll do a more in depth review, and play around with it a bit. |
Codecov ReportAttention: Patch coverage is
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #957 +/- ##
==========================================
+ Coverage 92.01% 92.07% +0.05%
==========================================
Files 108 109 +1
Lines 8240 8300 +60
==========================================
+ Hits 7582 7642 +60
Misses 658 658 ☔ View full report in Codecov by Sentry. |
@dantownsend |
@sinisaos Can you help me test this if you get some spare time? |
@dantownsend Sure. I will look into this during the day. |
@dantownsend Migrations are fine and constraints are correctly added to the database. The problems come after that when we try to query table. First error is File "/home/rkl/dev/unique_env/lib/python3.11/site-packages/piccolo/columns/base.py", line 831, in get_select_string
if self._alias:
^^^^^^^^^^^
AttributeError: 'UniqueConstraint' object has no attribute '_alias' If I prevent AttributeError with File "asyncpg/protocol/protocol.pyx", line 166, in prepare
asyncpg.exceptions.UndefinedColumnError: column task.task_constraints does not exist I think the main problem is that SELECT ALL "task"."id", "task"."name", "task"."description", "task"."completed", "task"."task_constraints" FROM "task" ORDER BY "task"."id" ASC |
Thank you for your support.
If you agree, I will close this PR and try again with this approach. |
@atkei First of all, thank you for your efforts. Extending the column class is tempting because it's easier to implement migrations (also the case in the original PR), but then we run into problems because Piccolo later treats unique constraints as a column that doesn't actually exist (and it shouldn't be like that), as I wrote in a previous comment.
It would be great if you could make a new PR and that approach seems much better to me (although I don't know how hard it is to implement). I'll be happy to try that new approach. Meanwhile we have an easy way to add and drop constraints using empty migration and raw sql like this # add constraints
# create empty migration with command `piccolo migrations new your_app_name`
async def run():
q = "ALTER TABLE task ADD CONSTRAINT task_constraints UNIQUE (name, description);"
await Task.raw(q)
# drop constraints
# create another empty migration with command `piccolo migrations new your_app_name`
async def run():
q = "ALTER TABLE task DROP CONSTRAINT task_constraints;"
await Task.raw(q) . Thanks again. |
@sinisaos Yeah, I use raw SQL in my project, so this feature is not mandatory:) |
@dantownsend @atkei I have a question for both of you. Did it make sense to add and drop unique constraints through the constraints.webmIt's just an idea, so please give your opinion. |
@sinisaos It's a nice idea. I do like having constraints defined in migrations, because when collaborating with other team members they get the constraints without having to do anything besides run the migrations. I think this PR has a lot of promise, but constraints were treated like a special form of column, which while elegant in some ways, might also be confusing. I think I'd prefer to treat them as totally different things if possible. |
@dantownsend You're right. I didn't think about that at all. I'm not a professional developer and have never worked in a team, but what you say absolutely makes sense if more than one person is working on the same project. Thanks for the clarification and forget about the |
Related to #172 , #175 and based on #582, #583.
UniqueConstraint
class with the same behavior as aColumn
to support auto migration with composite unique constraint