You get this error because you have trailing commas after your Column()
definitions, which cause application_essay_id
and theme_essay_id
to each be parsed as a one-element tuple containing a Column
instead of just a Column
. This stops SQLAlchemy from "seeing" that the columns are present, and consequently causes your model not to contain any primary key column.
If you simply replace
application_essay_id = db.Column(
db.Integer,
db.ForeignKey("application_essay.id"),
primary_key=True),
theme_essay_id = db.Column(
db.Integer,
db.ForeignKey("theme_essay.id"),
primary_key=True),
with
application_essay_id = db.Column(
db.Integer,
db.ForeignKey("application_essay.id"),
primary_key=True)
theme_essay_id = db.Column(
db.Integer,
db.ForeignKey("theme_essay.id"),
primary_key=True)
then your error will be fixed.
Aside: since SQLAlchemy (and Alembic and Flask-SQLAlchemy) contain some syntaxes for declaring models/tables that involve passing a comma-separated sequence of Column
s as arguments (e.g. to op.create_table()
or the Table()
constructor) and others that involve declaring a class with Column
s as class properties, it's really easy to run into this error by cutting and pasting Column
declarations from the first syntax to the second and forgetting to remove some of the commas. I suspect that this easy-to-make mistake is the reason this question has such a huge number of views - over 16000 at the time that I post this answer.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…