-
Notifications
You must be signed in to change notification settings - Fork 107
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
Use BigAutoField for User.id #826
base: master
Are you sure you want to change the base?
Conversation
We have been using the bigint data type for User.id since ab2573d, but the id field that Django automatically create for us uses AutoField, which maps to the int data type. This mismatch in data type is mostly benign, but when User.id grow large enough, it will blowup when we try to use setval on User.id (e.g. importing fixture with `manage.py loaddata`), with an error complaining value out of range.
Question for reviewers: can we omit |
Codecov Report
@@ Coverage Diff @@
## master #826 +/- ##
==========================================
+ Coverage 76.14% 76.33% +0.19%
==========================================
Files 70 70
Lines 2687 2688 +1
==========================================
+ Hits 2046 2052 +6
+ Misses 641 636 -5
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the migration work locally? Not sure if we need to do --dry-run
here. It should be good to go if this succeeds locally.
I remember it worked locally for me, but I'll check again (probably tomorrow) just to be sure. |
Migration works locally, and after the migration user creation still works. However, there's one change in the schema that might need to be reviewed (the diff is below); user.id is changed from @uranusjr what do you think we should do here? Maybe we can add a
Reference: --- before.sql 2020-07-26 04:19:21.733974888 +0000
+++ after.sql 2020-07-26 04:20:03.486554286 +0000
@@ -1080,11 +1080,11 @@
--
-- Name: users_user; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.users_user (
- id bigint DEFAULT public.users_user_id_generator() NOT NULL,
+ id bigint NOT NULL,
password character varying(128) NOT NULL,
last_login timestamp with time zone,
is_superuser boolean NOT NULL,
email character varying(255) NOT NULL,
speaker_name character varying(100) NOT NULL,
@@ -1140,11 +1140,10 @@
--
-- Name: users_user_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE public.users_user_id_seq
- AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
@@ -1326,10 +1325,17 @@
ALTER TABLE ONLY public.users_cocrecord ALTER COLUMN id SET DEFAULT nextval('public.users_cocrecord_id_seq'::regclass);
--
+-- Name: users_user id; Type: DEFAULT; Schema: public; Owner: postgres
+--
+
+ALTER TABLE ONLY public.users_user ALTER COLUMN id SET DEFAULT nextval('public.users_user_id_seq'::regclass);
+
+
+--
-- Name: users_user_groups id; Type: DEFAULT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.users_user_groups ALTER COLUMN id SET DEFAULT nextval('public.users_user_groups_id_seq'::regclass);
@@ -2251,15 +2257,15 @@
ALTER TABLE ONLY public.django_admin_log
ADD CONSTRAINT django_admin_log_content_type_id_c4bce8eb_fk_django_co FOREIGN KEY (content_type_id) REFERENCES public.django_content_type(id) DEFERRABLE INITIALLY DEFERRED;
--
--- Name: django_admin_log django_admin_log_user_id_c564eba6_fk_users_user_id; Type: FK CONSTRAINT; Schema: public; Owner: postgres
+-- Name: django_admin_log django_admin_log_user_id_c564eba6_fk; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.django_admin_log
- ADD CONSTRAINT django_admin_log_user_id_c564eba6_fk_users_user_id FOREIGN KEY (user_id) REFERENCES public.users_user(id) DEFERRABLE INITIALLY DEFERRED;
+ ADD CONSTRAINT django_admin_log_user_id_c564eba6_fk FOREIGN KEY (user_id) REFERENCES public.users_user(id) DEFERRABLE INITIALLY DEFERRED;
--
-- Name: events_customevent events_customevent_begin_time_id_ec21a279_fk_events_time_value; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
@@ -2355,15 +2361,15 @@
ALTER TABLE ONLY public.events_sponsoredevent
ADD CONSTRAINT events_sponsoredevent_end_time_id_3fa7d8a4_fk_events_time_value FOREIGN KEY (end_time_id) REFERENCES public.events_time(value) DEFERRABLE INITIALLY DEFERRED;
--
--- Name: events_sponsoredevent events_sponsoredevent_host_id_ffa9bf06_fk_users_user_id; Type: FK CONSTRAINT; Schema: public; Owner: postgres
+-- Name: events_sponsoredevent events_sponsoredevent_host_id_ffa9bf06_fk; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.events_sponsoredevent
- ADD CONSTRAINT events_sponsoredevent_host_id_ffa9bf06_fk_users_user_id FOREIGN KEY (host_id) REFERENCES public.users_user(id) DEFERRABLE INITIALLY DEFERRED;
+ ADD CONSTRAINT events_sponsoredevent_host_id_ffa9bf06_fk FOREIGN KEY (host_id) REFERENCES public.users_user(id) DEFERRABLE INITIALLY DEFERRED;
--
-- Name: proposals_additionalspeaker proposals_additional_proposal_type_id_cd3705a0_fk_django_co; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
@@ -2371,31 +2377,31 @@
ALTER TABLE ONLY public.proposals_additionalspeaker
ADD CONSTRAINT proposals_additional_proposal_type_id_cd3705a0_fk_django_co FOREIGN KEY (proposal_type_id) REFERENCES public.django_content_type(id) DEFERRABLE INITIALLY DEFERRED;
--
--- Name: proposals_additionalspeaker proposals_additionalspeaker_user_id_d1283d1e_fk_users_user_id; Type: FK CONSTRAINT; Schema: public; Owner: postgres
+-- Name: proposals_additionalspeaker proposals_additionalspeaker_user_id_d1283d1e_fk; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.proposals_additionalspeaker
- ADD CONSTRAINT proposals_additionalspeaker_user_id_d1283d1e_fk_users_user_id FOREIGN KEY (user_id) REFERENCES public.users_user(id) DEFERRABLE INITIALLY DEFERRED;
+ ADD CONSTRAINT proposals_additionalspeaker_user_id_d1283d1e_fk FOREIGN KEY (user_id) REFERENCES public.users_user(id) DEFERRABLE INITIALLY DEFERRED;
--
--- Name: proposals_talkproposal proposals_talkproposal_submitter_id_3e6b8833_fk_users_user_id; Type: FK CONSTRAINT; Schema: public; Owner: postgres
+-- Name: proposals_talkproposal proposals_talkproposal_submitter_id_3e6b8833_fk; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.proposals_talkproposal
- ADD CONSTRAINT proposals_talkproposal_submitter_id_3e6b8833_fk_users_user_id FOREIGN KEY (submitter_id) REFERENCES public.users_user(id) DEFERRABLE INITIALLY DEFERRED;
+ ADD CONSTRAINT proposals_talkproposal_submitter_id_3e6b8833_fk FOREIGN KEY (submitter_id) REFERENCES public.users_user(id) DEFERRABLE INITIALLY DEFERRED;
--
--- Name: proposals_tutorialproposal proposals_tutorialpr_submitter_id_4cb0d562_fk_users_use; Type: FK CONSTRAINT; Schema: public; Owner: postgres
+-- Name: proposals_tutorialproposal proposals_tutorialproposal_submitter_id_4cb0d562_fk; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.proposals_tutorialproposal
- ADD CONSTRAINT proposals_tutorialpr_submitter_id_4cb0d562_fk_users_use FOREIGN KEY (submitter_id) REFERENCES public.users_user(id) DEFERRABLE INITIALLY DEFERRED;
+ ADD CONSTRAINT proposals_tutorialproposal_submitter_id_4cb0d562_fk FOREIGN KEY (submitter_id) REFERENCES public.users_user(id) DEFERRABLE INITIALLY DEFERRED;
--
-- Name: reviews_review reviews_review_proposal_id_56d8777c_fk_proposals; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
@@ -2403,15 +2409,15 @@
ALTER TABLE ONLY public.reviews_review
ADD CONSTRAINT reviews_review_proposal_id_56d8777c_fk_proposals FOREIGN KEY (proposal_id) REFERENCES public.proposals_talkproposal(id) DEFERRABLE INITIALLY DEFERRED;
--
--- Name: reviews_review reviews_review_reviewer_id_29fc8a19_fk_users_user_id; Type: FK CONSTRAINT; Schema: public; Owner: postgres
+-- Name: reviews_review reviews_review_reviewer_id_29fc8a19_fk; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.reviews_review
- ADD CONSTRAINT reviews_review_reviewer_id_29fc8a19_fk_users_user_id FOREIGN KEY (reviewer_id) REFERENCES public.users_user(id) DEFERRABLE INITIALLY DEFERRED;
+ ADD CONSTRAINT reviews_review_reviewer_id_29fc8a19_fk FOREIGN KEY (reviewer_id) REFERENCES public.users_user(id) DEFERRABLE INITIALLY DEFERRED;
--
-- Name: reviews_talkproposalsnapshot reviews_talkproposal_proposal_id_e6e0fa3a_fk_proposals; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
@@ -2419,15 +2425,15 @@
ALTER TABLE ONLY public.reviews_talkproposalsnapshot
ADD CONSTRAINT reviews_talkproposal_proposal_id_e6e0fa3a_fk_proposals FOREIGN KEY (proposal_id) REFERENCES public.proposals_talkproposal(id) DEFERRABLE INITIALLY DEFERRED;
--
--- Name: users_cocrecord users_cocrecord_user_id_9c56481d_fk_users_user_id; Type: FK CONSTRAINT; Schema: public; Owner: postgres
+-- Name: users_cocrecord users_cocrecord_user_id_9c56481d_fk; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.users_cocrecord
- ADD CONSTRAINT users_cocrecord_user_id_9c56481d_fk_users_user_id FOREIGN KEY (user_id) REFERENCES public.users_user(id) DEFERRABLE INITIALLY DEFERRED;
+ ADD CONSTRAINT users_cocrecord_user_id_9c56481d_fk FOREIGN KEY (user_id) REFERENCES public.users_user(id) DEFERRABLE INITIALLY DEFERRED;
--
-- Name: users_user_groups users_user_groups_group_id_9afc8d0e_fk_auth_group_id; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
@@ -2435,32 +2441,16 @@
ALTER TABLE ONLY public.users_user_groups
ADD CONSTRAINT users_user_groups_group_id_9afc8d0e_fk_auth_group_id FOREIGN KEY (group_id) REFERENCES public.auth_group(id) DEFERRABLE INITIALLY DEFERRED;
--
--- Name: users_user_groups users_user_groups_user_id_5f6f5a90_fk_users_user_id; Type: FK CONSTRAINT; Schema: public; Owner: postgres
---
-
-ALTER TABLE ONLY public.users_user_groups
- ADD CONSTRAINT users_user_groups_user_id_5f6f5a90_fk_users_user_id FOREIGN KEY (user_id) REFERENCES public.users_user(id) DEFERRABLE INITIALLY DEFERRED;
-
-
---
-- Name: users_user_user_permissions users_user_user_perm_permission_id_0b93982e_fk_auth_perm; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.users_user_user_permissions
ADD CONSTRAINT users_user_user_perm_permission_id_0b93982e_fk_auth_perm FOREIGN KEY (permission_id) REFERENCES public.auth_permission(id) DEFERRABLE INITIALLY DEFERRED;
--
--- Name: users_user_user_permissions users_user_user_permissions_user_id_20aca447_fk_users_user_id; Type: FK CONSTRAINT; Schema: public; Owner: postgres
---
-
-ALTER TABLE ONLY public.users_user_user_permissions
- ADD CONSTRAINT users_user_user_permissions_user_id_20aca447_fk_users_user_id FOREIGN KEY (user_id) REFERENCES public.users_user(id) DEFERRABLE INITIALLY DEFERRED;
-
-
---
-- PostgreSQL database dump complete
-- |
Or we can subclass |
I guess another option is to add a But I'd like to avoid this option if possible, and rather have things declared in our Django model. |
This could work (not tested) from psycopg2.extensions import AsIs
id = models.BigAutoField(primary_field=True, default=AsIs('public.users_user_id_generator()')) |
Types of changes
Description
We have been using the
bigint
data type for User.id since ab2573d, but the id field that Django automatically create for us uses AutoField, which maps to theint
data type.This mismatch in data type is mostly benign, but when User.id grow large enough, it will blowup when we try to use
setval
on User.id (e.g. importing fixture withmanage.py loaddata
), with an error complaining value out of range.Steps to Test This Pull Request
Steps to reproduce the behavior:
python manage.py loaddata fixtures.json
Expected behavior
Importing fixture data succeed without error
Related Issue
None that I know of.
More Information