Comments (8)
Interesting. More-or-less what you're asking is the point of this library: to provide binary UUIDs under an integer id compatible interface.
Could you post here what errors you're seeing? We've tried as best we can to confirm to the "normal" ActiveRecord conventions to make this possible.
from activeuuid.
I created a DB migration to convert the ID column to UUID, included ActiveUUID::UUID in my model, restarted webrick and then tried to load a record with an old ID (in this case, 82). Relevant lines from Rails log:
Parameters: {"utf8"=>"✓", "show"=>{"id"=>"82"}, "id"=>"presentation_report"}
Show Load (0.5ms) SELECT shows
.* FROM shows
WHERE shows
.id
= x'00000000000000000000000000003832' LIMIT 1
Completed 404 Not Found in 98ms (ActiveRecord: 7.2ms)
ActiveRecord::RecordNotFound (Couldn't find Show with 'id'=82):
My migration was just this one liner:
change_column :shows, :id, :uuid
IDs were appearing in their integer form when looking at the table in Sequel Pro. Perhaps I missed an installation step?
from activeuuid.
I haven't looked at this closely, but I don't think that a UUID conversion will keep your old IDs. E.g. in that request above, I don't think you'll be able to look up {"id" => "82"}
and have that map to the UUID you're expecting.
You might want to try keeping your id
column as an integer and then create an additional uuid
column. Then re-save each record (which will create a uuid for each one) and then use the uuid column in that way.
from activeuuid.
@crypticsymbols Look at using for_each
in your migration, which will load in batches of records at about ~1000 at a time but without running out of memory. For each one, you can just replace the existing id with a randomly generated ID from running SecureRandom.uuid
.
from activeuuid.
Well, the trouble is we need to keep the old integer IDs. We can certainly re-save each record, but it doesn't seem to want to take an integer value. After the migration:
2.2.1 :012 > s.id
=> #<UUID:0x3fd0d35be0a8 UUID:38320000-0000-0000-0000-000000000000>
2.2.1 :013 > s.id = 82
=> 82
2.2.1 :014 > s.id
=> nil
Honestly I wasn't really expecting that to work ;). I also tried creating a new UUID column, setting that to an integer on a test, and then querying where uuid = 82 - but still no dice. Do I need to cast or convert it somehow?
Unless there's something obvious I'm missing, I don't want to take up too much time. We can deal with the performance hit of using string PKs.
from activeuuid.
Yes, what you're missing is that you can't store an integer in a uuid field. So, it sounds like you'll be best off adding a new column, generating uuids for that column, and then changing your primary key to be the uuid column.
from activeuuid.
Ah, gotcha. I misinterpreted the first comment about backwards compatibility. Thanks guys!
from activeuuid.
So the problem is that integer ids and binary ids aren't really interchangeable in that sort of "straightforward" way. You can keep your integer ids and uuids by doing the following:
- Revert back to your original state where you have your regular
id
s as integers (e.g. how you had it before you started usingactiveuuid
- Create a migration that adds a
uuid
column, and make theuuid
column the binary uuid.
If you do this, you'll be able to find by id in your old way and then also find by uuid using the new uuids.
In the past, I've even gone so far as to patch .find
to auto-detect whether it's an integer id or uuid and then automatically pick the right finder. (I think early versions of this plugin had that. But it isn't 100% reliable so something like that isn't in core.)
from activeuuid.
Related Issues (20)
- How to add uuid primary key column to the existing table in migration script
- mysql2 adapter not loaded when railtie runs HOT 3
- Problems with the search by id
- Primary key column_hash is binary != :uuid HOT 2
- Default primary key value is incompatible with accepts_nested_attributes_for HOT 1
- has_one children being updated with parent record HOT 1
- Finding non-existent record in assocation causes Cannot visit UUIDTools::UUID with Rails 3
- to_yaml support & yaml_db clean export
- Still Maintained? HOT 1
- 0.6.1 patch release? HOT 2
- PostgreSQL: TypeError: can't cast UUIDTools::UUID to uuid HOT 5
- you can't redefine the primary key column 'id'. HOT 3
- Rails 5 compatibility HOT 8
- SQL (MySQL) errors in complex arel queries HOT 1
- No Changelog
- Rails 5 issues HOT 5
- schema.rb not being generated correctly after migration HOT 4
- .find not accepting string containing UUID HOT 1
- .includes association with uuid not working HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from activeuuid.