0

I have a table where I have 2 pieces of information whose combination must always be unique.

By definition, if we look at these two columns for any given row, they are unique and should be primary or at least a candidate key, but in my case they are primary.

However, when I try to create the table with Flask SQLAlchemy I am told that I cannot create a table without a primary key.

How do I specify that these two compound keys should be treated as a primary key?

Edit:

Upon reading my question I thought it could use a bit more pratical description:

Givent table:

A | B
-----
5 | 1
5 | 2
5 | 3
5 | 4
5 | 5
6 | 1

We can se that no two combinations of A and B occurred. That is why I am trying to set A and B as a primary key instead of having something like:

id | A | B 
-----------
 1 | 5 | 1
 2 | 5 | 2
 3 | 5 | 3
 4 | 5 | 4
 5 | 5 | 5
 6 | 6 | 1

which just wastes memory.

no_modules
  • 117
  • 7

1 Answers1

0

From the question it sounds like you're not declaring any column as the primary key, however you can declare primary_key=True on multiple columns to create a composite primary key, from the SQLAlchemy docs: (the second code example shows this in action)

https://docs.sqlalchemy.org/en/14/faq/ormconfiguration.html#how-do-i-map-a-table-that-has-no-primary-key

This previous SO question might also help:

How to define composite primary key in SQLAlchemy