On 07/29/2016 09:45 PM, Jason Libbey wrote:
Hi, this is my first post, so if it does not adhere by your guidelines,
please let me know and I'll fix it ASAP.

I'm using the sqlalchemy_utils.UUIDType as per
backend-agnostic-guid-type
<http://docs.sqlalchemy.org/en/latest/core/custom_types.html#backend-agnostic-guid-type>.

OK



from sqlalchemy_utils import UUIDType

OK that is something else, UUIDType is probably taken from the recipe here but if you're importing that from the third-party sqlalchemy_utils as opposed to typing out the GUID type directly, then the change I'm giving you below is not possible, you need to either talk to them or use the recipe type directly.






class ApplicationStore(Base, Timestamp):

    __tablename__ = 'applications'

id = Column(UUIDType(binary=False), primary_key=True, default=uuid.uuid4)

    user_uuid = Column(UUIDType(binary=False), unique=True, index=True, 
nullable=False)



Since I am using a mysql db, the UUIDType is correctly falling back to
the CHAR32 representation.  However, the value that gets written to the
db is the uuid with no dashes.

that's because the recipe is using the int value of the object (though I don't know what the 'binary' flag on the sqlalchemy_utils version does):

>>> some_uuid = str(uuid.uuid4())
>>> print some_uuid
57e8077f-0f12-4d0f-9ef6-2a45ac70a4bf

>>> print "%.32x" % uuid.UUID(some_uuid).int
57e8077f0f124d0f9ef62a45ac70a4bf




application = application_service.create(user_uuid=uuid.uuid4())



The string representation of the uuid shows it with dashes, but the
database char is saved without dashes.  So my questions are:
1. Am I using this correctly?

probably, sqlalchemy_utils person would know for sure

2. Is this the expected behavior?

yes


3. How can I force it to save as uuid with dashes, while still using the
backend agnostic guid type?

first question is, why. the dashes just take up useless space. Let's assume you want MySQL console behavior to print the dashes without you having to do anything to the value, OK. use the GUID recipe from the documentation, change it to use char(36) (four more characters to save the dashes) and change the string conversion part to just use str(uuid) instead of the above "%.32x" conversion.




Python version: 2.7.10
SQLAlchemy version: 1.0.12

--
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to