Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Line 56 is executed as the file is loaded. Simplified, the line is essentially:

  id = Column(default=str(uuid.uuid4()))

As written, a UUID is generated once and used to set a class-level attribute. Each Python process would generate a unique value, so it wouldn't be immediately obvious. Most of the time Python's ability to run code as a file is loaded is helpful, but this is one the well known gotchas.

Although I'm not a SQL Alchemy user, I assume the fix is essentially the same as it would be for Django. So the correct code would have been essentially:

  id = Column(default=uuid.uuid4)

Instead of executing `uuid4()` and caching a single UUID value, it would have executed `uuid4()` each time a new object was created.


default=lambda: str(uuid.uuid4())


ChatGPT, rewrite this as a named function

Sure,

```

UUID4 = uuid.uuid4()

def default_id():

    return UUID4
```




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: