Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8097219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:41:15+00:00 2026-06-05T21:41:15+00:00

Say I have the following class: class User(Base): __tablename__ = ‘users’ id = Column(Integer,

  • 0

Say I have the following class:

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    public_key = Column(String(16), nullable=False, unique=True)
    private_key = Column(String(64), nullable=False, unique=True)

I need to have both public_key and private_key be random strings (which is not an issue) but I’m not sure the best way to make sure that

  1. the strings don’t already exist anywhere else
  2. between checking for their existence and creating the object, they haven’t already been created by another process.

I know I can’t be the first person to have come across this but I haven’t been able to find much on this. It seems that SQLAlchemy doesn’t actually enforce unique itself so I’m not sure what to do. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T21:41:16+00:00Added an answer on June 5, 2026 at 9:41 pm

    Realize the chances of collision are very, very, very low.

    Let’s say your random strings are composed up uppercase letters and numerals only. That’s 36 possibilities per character. The 16 characters of your public key will net you 36^16 combinations. That’s about 8 with 24 0s after it. Even if you have a million keys in your database, the chances of collision when you add a new key are 10^-19.

    I wouldn’t worry about it. I don’t, and the random values I use (UUID4, for the most part) have a much higher chance of collision than yours. (But still astronomically low.)

    If you’re still concerned: SQLAlchemy may not enforce UNIQUE, but your DBMS should, which means SQLAlchemy will throw an exception if the UNIQUE constraint is violated. Can catch the exception and degrade gracefully.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say we have following models. class User(db.Model): username=db.StringProperty() avatar=db.ReferenceProperty() class User(db.Model): username=db.StringProperty() avatar=db.StringProperty()
Let's say I have the following class hierarchy in C++: class Base; class Derived1
Ok say I have the following model: class Country < ActiveRecord::Base validates_presence_of :name validates_presence_of
Let's say I have following POJO's using Hibernate. public class User { private String
Say i have the following two classes: public class User { public int ID
Say I have the following model: class Person(models.Model): street_address = models.CharField(max_length=50, blank=True) suburb =
Let's say I have the following models: Class Wishlist belongs_to :user # User class
Lets say I have the following one-to-many relationship: Site has many Users User belongs
Let’s say you have the following mongoid documents: class User include Mongoid::Document embeds_one :name
Let us say we have the following scenario: class MyModel < ActiveRecord::Base after_save :throw_after_save

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.