ITADN

Implement a no-primary-key option for tables

#1409Closedraisfeld-ori 创建于 2025-11-30
R
raisfeld-oricommented
Implementing a table without a primary key [is possible](https://stackoverflow.com/questions/34283259/how-to-define-a-table-without-primary-key-with-sqlalchemy), however, the solution is messy and really confusing when reading the code. I suggest adding a new boolean variable to ```SQLAlchemy().Model``` called "no_primary_key", to handle the case where there's no primary key. Example: ```py from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() # The old implementation class MyOldModel(db.Model): fake_primary_key = db.Column(db.Integer, primary_key=True) ... # Rest of the model # The suggested new implementation class MyNewModel(db.Model): no_primary_key = True ... # Rest of the model ``` This change won't break old code, since the variable would be set to False by default, and you would need to intentionally define it for the change to be active. I plan on implementing this feature myself, but I thought I would first suggest it before to see if the maintainers think this is a good idea or not.
关闭于 2025-11-30 9 条评论