I want to reference to an entity that defined in code (eg: default product with id=-1), but currently module entity reference does not allow reference to entity with id =-1 (out of range)
I use "-1" for my default product because it is unique, doesn't conflict with other product (created by user, stored in database)
I think module entityreference allow referrence to entity with negative id number. We can write custom validate for other entity type if necessary

CommentFileSizeAuthor
#1 1969336.patch2.21 KBzkday

Comments

zkday’s picture

Status: Active » Needs review
StatusFileSize
new2.21 KB

This is a patch. ;)

jasonlttl’s picture

Issue summary: View changes

Just a quick me too...

At our university, we have a system for integrating remote entities from different data sources (HR, student information systems, faculty tenure, etc). We automagically pull these in as locally cached entities. However, our users needed a way to add local override records without going through red tape. So we added the ability to create local entity records. To avoid the potential for id conflicts between remote records and local records, we made the local ids negative just like zkday did above... and then we hit this issue.

As an aside, there's an interesting issue on the field api needing to support textual ids for entities.
https://www.drupal.org/node/1823494

I haven't tested the patch yet, but looking at it, I'd think you'd want to go to bigint instead of just flipping the signed to unsigned (and keeping 4 bit ints). That way, if anyone has ids between 2^31 and 2^32 (2-4 billion), they will continue to work. That said, I would guess that doubling the size of every entity ref field from 4 bytes to 8 bytes could also be something to be careful about.

These are the "standard" and "big" sizes for mysql and probably everything else.
signed 4 bits : -2147483648 to 2147483647
unsigned 4 bits : 0 to 4294967295
signed 8 bits : -9223372036854775808 to 9223372036854775807
unsigned 8 bits : 0 to 18446744073709551615

Another strategy I'm looking at is segregating remote ids and local ids within the positive space.
< 2^31 means remote
> 2^31 means local

This would effectively be the same address space as changing to signed 4 bit numbers but there are some downsides with respect to clarity and collision handling.