I currently have a date dim table - meaning every record has a unique date tied to it. There will never be a record with the same date, and the concept of this table is to never be updated once initial data is added. Think - calendar table.
I have an instance where it would make sense to have a date_dim table where the PK is a date data type. Then have an event table tied back to the date_dim using an FK tied to the PK. Is it possible to set the data type of a Foreign Key field in django models?
Or is this an instance where I would need to set managed=false and then just set the FK up on the database end myself?
I am talking about the DB Column and the DB data type. A FK is associated with a column - and that column is going to have a datatype associated with it.
When I tried the above scenario - django complained because it tried to make my FK an int rather than a date.
As far as my premise being wrong, you’re going to have to be a bit more clear. because what I am seeing is django trying to give the following an int datatype.
The above was all created from nothing. Keep in mind, I am not really trying to solve anything. I think that might help steer your thinking. If you look at the above class - that is literally how it is in the database. I created a new app and used the above model along with;
You;re saying - django should have looked at the PK in Calendar and just automatically made calendar_event.calendar_id a Date datatype for me? Because it gave me an error saying it was trying to convert it to an int. If that is what you are saying I can try it again.
I guess the only possible caveat to that would be a database that doesn’t support non-integer primary keys - but I don’t know of any with that limitation.
The other possibility would be a database that doesn’t have an intrinsic date data type - one that stores a date as an integer. Again, I don’t immediately know of any that restricted.
You define a primary key using the primary_key attribute. The data type of that column is going to be whatever the column is defined as. If you define a date field as the primary_key, the primary key column is going to be a date type.
It may have been clearer had I written:
The column in the database table being used as the primary key is the data type of the model field assigned as the primary key.