最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]SQLAlchemy中给列的id从数字变成UUID

SQLAlchemy crifan 6081浏览 0评论

之前已经在Flask中的SQLAlchemy的某列中,用的id都是integer数字:

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/sipevents/sipevents/models.py

class Event(db.Model):
    __tablename__ = “events”
    # Columns
    id = db.Column(db.Integer, primary_key = True, autoincrement = True, nullable=False)

对应的活动连接地址也是简单的:

xxx/show_event/1

xxx/show_event/23

现在希望:变成UUID那种-》否则别的恶意的人,看到这个event的id,或者虽然找个数字,就容易去搞破坏了。

想起来了:

别人设计的,有类似于:

event-xxxx-xxxx-xxxx-xxxx

user-xxxx-xxxx-xxxx-xxxx

后来才发现,原来不是4个4位的字母或数字,而是:

user-e85906ff-aca6-457f-af46-793e46b51c71

user-8位-4位-4位-4位-12位

共32位

尽量考虑做成这样。

数据库 ID uuid

MySQL 用 UUID 作为主键,实际使用中有什么问题 · Ruby China

数据库的唯一标示符(ID)的选择 – 永远的朋友 – 51CTO技术博客

数据库 ID 选择 设计

数据库设计 – 设计数据库(MYSQL),如何选择自增主键,还是GUID? – SegmentFault

数据库设计时的一些细节的东西如何处理? – 知乎

数据库设计之主键的思考 .-douyaqiang123-ChinaUnix博客

为什么是UUID做主键 – highriver – 博客园

mysql 主键选择 – 楚 – 博客园

UUID做主键,好还是不好?这是个问题 (转)_无尽空虚_新浪博客

Mysql 用UUID做主键可行么?一直想尝试,但没条件测试。 – 知乎

数据库的唯一标示符(ID)的选择 – 永远的朋友 – 51CTO技术博客

[整理]数据库设计主键时UUID和GUID的区别

那就去选择:

GUID或UUID吧

SQLAlchemy uuid

Custom Types — SQLAlchemy 1.1 Documentation

python – How can I use UUIDs in SQLAlchemy? – Stack Overflow

python – How to use UUIDs instead of integers in MySQL DB – Stack Overflow

sqlalchemy_utils.types.uuid — SQLAlchemy-Utils 0.32.9 documentation

UUID in SQLAlchemy — 你好!我是罗小能。

使用 sqlalchemy 时如何配置使用 postgresql 的 uuid 类型 – Mozillazg’s Blog

flask_sqlalchemy.SQLAlchemy.UUID – Nullege Python Samples

Nullege: A Search Engine for Python source code

先写成:

import uuid
class Event(db.Model):
    __tablename__ = “events”
    id = db.Column(db.UUID, primary_key=True, default=uuid.uuid4, nullable=False)

待后续确认是否OK。

结果出错;

Traceback (most recent call last):
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/arbiter.py”, line 557, in spawn_worker
    worker.init_process()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/base.py”, line 126, in init_process
    self.load_wsgi()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/base.py”, line 136, in load_wsgi
    self.wsgi = self.app.wsgi()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/app/base.py”, line 67, in wsgi
    self.callable = self.load()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py”, line 65, in load
    return self.load_wsgiapp()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py”, line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/util.py”, line 357, in import_app
    __import__(module)
  File “/root/html/SIPEvents/run.py”, line 1, in <module>
    from sipevents import app
  File “/root/html/SIPEvents/sipevents/__init__.py”, line 65, in <module>
    from sipevents import views, models
  File “/root/html/SIPEvents/sipevents/views.py”, line 38, in <module>
    from . import models
  File “/root/html/SIPEvents/sipevents/models.py”, line 91, in <module>
    class Event(db.Model):
  File “/root/html/SIPEvents/sipevents/models.py”, line 96, in Event
    id = db.Column(db.UUID, primary_key=True, default=uuid.uuid4, nullable=False)
AttributeError: ‘SQLAlchemy’ object has no attribute ‘UUID’
[2016-09-10 16:25:25 +0000] [2561] [INFO] Worker exiting (pid: 2561)

搜:

AttributeError: ‘SQLAlchemy’ object has no attribute ‘UUID’

AttributeError SQLAlchemy  object has no attribute UUID

zzzeek / sqlalchemy / issues / #3323 – In 0.9.9, uuid columns are broken with postgresql+psycopg2 — Bitbucket

python – AttributeError: ‘int’ object has no attribute ‘_sa_instance_state’ – Stack Overflow

sqlalchemy学习笔记

[整理]Python中的uuid.uuid4()

[已解决]SQLAlchemy设置默认值出错:sqlalchemy.exc.ArgumentError: Unknown arguments passed to Column: [‘defaut’]

另外,有空再去用:

Backend-agnostic GUID Type

和:

sqlalchemy_utils.types.uuid — SQLAlchemy-Utils 0.32.9 documentation

SQLAlchemy GUID  用法

目前的代码是:

# from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.sql import func
import uuid
def generateUUID(prefix = “”):
    generatedUuid4 = uuid.uuid4()
    generatedUuid4Str = str(generatedUuid4)
    newUuid = prefix + generatedUuid4Str
    gLog.debug(“prefix=%s, generatedUuid4Str=%s, newUuid=%s”, prefix, generatedUuid4Str, newUuid)
    return newUuid
class Event(db.Model):
    __tablename__ = “events”
    # Columns
    #id = db.Column(db.Integer, primary_key = True, autoincrement = True, nullable=False)
    #id = db.Column(db.String(64), primary_key=True, default=uuid.uuid4().hex, nullable=False)
    # id = db.Column(db.String(64), primary_key=True, default = generateUUID(“event-“), nullable=False)
    # id = db.Column(db.String(64), primary_key=True, server_default = generateUUID(“event-“), nullable=False)
    # id = db.Column(db.String(64), primary_key=True, default= (“event-%s” % uuid.uuid4()), nullable=False)
    id = db.Column(db.String(64), primary_key=True, default = generateUUID(“event-“), nullable=False)

输出:

[2016-09-10 17:55:24,784 DEBUG models.py:98 generateUUID] prefix=event-, generatedUuid4Str=72c00327-1021-41f4-ba8a-6b6e788fc3fe, newUuid=event-72c00327-1021-41f4-ba8a-6b6e788fc3fe
[2016-09-10 17:55:25,007 DEBUG views.py:320 getEventInfo] curEvent=<Event:id=event-6c316a40-1f14-495a-a7af-80c2febe31df,title=u’\u4eca\u65e5′,user_openid=oswjmv-QiQp-_5pFB0thKxfCZ8Tc>
[2016-09-10 17:55:25,007 DEBUG views.py:376 notifyEvent] eventId=event-6c316a40-1f14-495a-a7af-80c2febe31df, eventToNotif=<Event:id=event-6c316a40-1f14-495a-a7af-80c2febe31df,title=u’\u4eca\u65e5′,user_openid=oswjmv-QiQp-_5pFB0thKxfCZ8Tc>, notifDatetimeGmt=2016-09-10 09:55:00
[2016-09-10 17:55:25,017 DEBUG views.py:379 notifyEvent] eventCreator=<User nickname=u’crifan.com’ openid=oswjmv-QiQp-_5pFB0thKxfCZ8Tc avatar_static_path=img/avatar/oswjmv-QiQp-_5pFB0thKxfCZ8Tc.png>

转载请注明:在路上 » [已解决]SQLAlchemy中给列的id从数字变成UUID

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.186 seconds, using 22.13MB memory