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 6054563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:10:01+00:00 2026-05-23T08:10:01+00:00

On the database side, I gather that a natural primary key is preferable as

  • 0

On the database side, I gather that a natural primary key is preferable as long as it’s not prohibitively long, which can cause indexing performance problems. But as I’m reading through projects that use sqlalchemy via google code search, I almost always find something like:

class MyClass(Base):
    __tablename__ = 'myclass'
    id = Column(Integer, primary_key=True)

If I have a simple class, like a tag, where I only plan to store one value and require uniqueness anyway, what do I gain through a surrogate primary key, when I’m using sqlalchemy? One of the SQL books I’m reading suggests ORM’s are a legitimate use of the ‘antipattern,’ but the ORMs he envisions sound more like ActiveRecord or Django. This comes up a few places in my model, but here’s one:

class Tag(Base):
    __tablename__ = 'tag'
    id = Column(Integer, primary_key=True) #should I drop this and add primary_key to Tag.tag?
    tag = Column(Unicode(25), unique=True) 
    ....

In my broader, relational model, Tag has multiple many-to-many relationships with other objects. So there will be a number of intermediate tables that have to store a longer key. Should I pick tag or id for my primary key?

  • 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-05-23T08:10:01+00:00Added an answer on May 23, 2026 at 8:10 am

    Although ORMs or programming languages make some usages easier than others, I think that choosing primary key is a database design problem unrelated to ORM. It is more important to get database schema right on its own grounds. Databases tend to live longer than code that accesses them, anyways.

    Search SO (and google) for more general questions on how to chose primary key, e.g.: https://stackoverflow.com/search?q=primary+key+natural+surrogate+database-design ( Surrogate vs. natural/business keys, Relational database design question – Surrogate-key or Natural-key?, When not to use surrogate primary keys?, …)


    I assume that Tag table will not be very large or very dynamic.
    In this case I would try to use tag as a primary key, unless there are important reasons to add some invisible to end user primary key, e.g.:

    • poor performance under real world data (measured, not imagined),

    • frequent changes of tag names (but then, I’d still use some unique string based on first used tag name as key),

    • invisible behind-the-scenes merging of tags (but, see previous point),

    • problems with different collations — comparing international data — in your RDBMS (but, …)

    • …


    In general I observed that people tend to err in both directions:

    • by using complex multi-field “natural” keys (where particular fields are themselves opaque numbers), when table rows have their own identity and would benefit from having their own surrogate IDs,

    • by introducing random numeric codes for everything, instead of using short meaningful strings.

    Meaningful primary key values — if possible — will prove themselves useful when browsing database by hand. You won’t need multiple joins to figure out your data.

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

Sidebar

Related Questions

I have a SQL Server 2008 database table that uses uniqueidentifier as a primary
I've been trying to design a database schema for a side project but I
Database has tables Photos and PhotoAlbums. I need a query that will select all
Which Database table Schema is more efficient and why? Users (UserID, UserName, CompamyId) Companies
A database application that I'm currently working on, stores all sorts of settings in
What database should a beginner use in the C language? Can I use MySQL?
The Database Tuning Advisor is recommending that I create a bunch of statistics in
I want to do a database-side string concatenation in a Rails query, and do
I am relatively new to .Net and the database side of it in particular.
I am building a database that contains public, private(limited to internal) and confidential data

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.