-
Notifications
You must be signed in to change notification settings - Fork 263
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
Cannot cast drive.start_field and drive.end_field to integer or smallint #45
Comments
This sounds like a great project! Thank you for working on it. Bringing With respect to your problem... The issue is that the This selects the full "row": (notice the parenthesis, you could also think of SELECT start_field FROM drive LIMIT 1;
start_field
-------------
(-30)
(1 row) And you can access the individual components of the row or tuple: SELECT (start_field).pos FROM drive LIMIT 1;
start_field
-------------
-30
(1 row) And now you can cast: SELECT (start_field).pos::int FROM drive LIMIT 1;
start_field
-------------
-30
(1 row) The issue is that you were trying to cast a tuple to an integer directly. This problem would have been more clear if it weren't a 1-tuple, but creating custom types is useful for automatic serialization with Python's PostgreSQL driver, |
Hi -- thanks, that was a very helpful explanation! It makes sense now why some fields are encapsulated by parentheses -- they're tuples with only one element! So I can cast ALTER TABLE drive ALTER COLUMN start_field SET DATA TYPE int USING (start_field).pos::int;
ERROR: column notation .pos applied to type integer, which is not a composite type
LINE 1: ...BLE drive ALTER COLUMN start_field SET DATA TYPE int USING (...
^ My guess is that this ALTER statement is trying to alter the column (a tuple) and change the data type by using an individual component of the tuple, instead of the tuple itself. But all I want is the component of the tuple, not the 'row' itself (composite type is to row/record as data type is to field?). Perhaps I'm not understanding how ALTER TABLE drive ALTER COLUMN start_field SET DATA TYPE int USING (start_field).pos;
ERROR: column notation .pos applied to type integer, which is not a composite type
LINE 1: ...BLE drive ALTER COLUMN start_field SET DATA TYPE int USING (...
^ ALTER TABLE drive ALTER COLUMN start_field TYPE int USING (start_field).pos;
ERROR: column notation .pos applied to type integer, which is not a composite type
LINE 1: ...BLE drive ALTER COLUMN start_field TYPE int USING (start_fie...
^ Any thoughts? |
I'm stumped. Can you brute force it instead? nfldb_test=# alter table drive add column start_field_scalar field_offset;
ALTER TABLE
nfldb_test=# update drive set start_field_scalar = (start_field).pos;
UPDATE 39368
nfldb_test=# alter table drive drop column start_field;
ALTER TABLE
nfldb_test=# alter table drive rename column start_field_scalar to start_field;
ALTER TABLE Resulting table:
|
Sorry for the late response, but I will resort to adding/dropping columns if I can't figure out another way. Feel free to close this if you want to keep the issue list with recent or high priority issues. |
Thanks again for the help! |
No worries, I'm just doing some tidying. Feel free to close this when you reach a resolution. I noodled on this a little more, and I speculate that it may be impossible to do with a single In any case, I found it difficult to get proper documentation on the matter. The |
So is it possible to do this with multiple |
If my speculation about I emphasize that this is speculation. Haven't really had time to dig deeper. The method I suggested above is a bit brutish, but if it's contained inside a single transaction, then it's guaranteed that you won't corrupt your database by being left in an intermediate state. (You should be thankful that you aren't dealing with SQLite. It doesn't even have an If you're writing migrations and it's doing transactions correctly, then there really isn't a major downside to just using my approach. In fact, it's probably pretty close to what happens under the hood if the |
I'm looking to make some schema and data type tweaks to nfldb so that I can more easily serve nfldb data to Rails' Active Record models. I'm doing just fine with automatically casting user-defined data types to varchars, like
player.position
,player.status
,game.day_of_week
, and so on. But it seems that I'm not having much luck with the numeric user-defined data types for fields likedrive.start_field
,drive.end_field
, anddrive.pos_time
.Is there any way to do this? Here's what I've tried so far:
So I tried specifying a USING expression, but here's the error:
It seems that I need to go one level deeper, not just setting the data type for the user-defined type
field_pos
, but also for the domain constraintfield_offset
.The business reason for altering data types is that Rails Active Record Migrations can't recognize user-defined types in PostgreSQL, so won't know to interpret
start_field
andend_field
as integers. I'm making a bunch of other table and column alters too, and have taken care to write changes to revert to the original schema so thatnfldb-update
still works.Thanks in advance!
The text was updated successfully, but these errors were encountered: