In the automatically generated model file, the self-incrementing id field does not have the `autoincrement=true` attribute
Joker-desire opened this issue · 5 comments
Things to check first
-
I have searched the existing issues and didn't find my bug already reported there
-
I have checked that my bug is still present in the latest release
Sqlacodegen version
2.3.0
SQLAlchemy version
1.4.13
RDBMS vendor
MySQL (or compatible)
What happened?
In the automatically generated model file, the self-incrementing id field does not have the autoincrement=true
attribute。
Add new data to get the self-incrementing id
def add_by_schemas(db: Session, param: DailyStatisticSchemas):
daily = DailyStatistic(**dict(param))
db.add(daily)
db.commit()
return daily.id
error
sqlalchemy.orm.exc.ObjectDeletedError: Instance '<DailyStatistic at 0x1376a7b20>' has been deleted, or its row is otherwise not present.
Database schema for reproducing the bug
Database Table
CREATE TABLE `daily_statistics` (
`id` int(11) NOT NULL AUTO_INCREMENT,
...
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;
automatically generated model
class DailyStatistic(Base):
__tablename__ = 'daily_statistics'
id = Column(INTEGER(11), primary_key=True, nullable=False)
A single-column integer PK is automatically made into an autoincrement column by default:
The default value is the string "auto", which indicates that a single-column (i.e. non-composite) primary key that is of an INTEGER type with no other client-side or server-side default constructs indicated should receive auto increment semantics automatically.
I don't see what that error has to do with autoincrement.
Or was the problem that autoincrement was supposed to be 10, not 1?
The id defined by the data table is self-incrementing:int(11) NOT NULL AUTO_INCREMENT
But the generated model has no incremental configuration:id = Column(INTEGER(11), primary_key=True, nullable=False)
Normally it should generate:id = Column(INTEGER(11), primary_key=True, nullable=False, autoincrement=True)
Shouldn't that be the case? boss
I already explained that a single-column integer-typed primary key gets autoincrement turned on automatically: https://docs.sqlalchemy.org/en/20/core/metadata.html#sqlalchemy.schema.Column.params.autoincrement
What is the actual problem you have with this?
Oh, thanks, I misunderstood this usage myself, I'm very sorry to have caused you trouble.