Hi Guys!
I have ORMs with autoincrement primary keys, and I'd like to achive a
multiple insert with sqlalchemy.
I'm making a lot of objects and adding them to the session, and after I'm
done, I'd like to make session.commit() but then I get this error:
(MySQLdb.IntegrityError) (1062, "Duplicate entry '0' for key 'PRIMARY'").
Some example code.
class Base(DeclarativeBase):
pass
class Partner(Base):
__tablename__ = 'partner'
id: Mapped[int] = mapped_column(Integer, primary_key=True,
autoincrement=True)
name: Mapped[Optional[str]] = mapped_column(String(255,
collation='utf8mb4_unicode_ci'), nullable=True, default=None)
address: Mapped[Optional[str]] = mapped_column(String(255,
collation='utf8mb4_unicode_ci'), nullable=True, default=None)
class ImportHandler:
def __init__(self, db_name: str):
plain_engine = create_engine(os.getenv('db_connection') + db_name +
'?charset=utf8mb4', echo='debug')
self.session = Session(plain_engine)
def import(self):
for i in range(10):
partner = Partner()
partner.name = "Some"
partner.address = "Test"
self.session.add(partner)
self.session.commit()
Can anybody explain that how can I solve this?
Thank you very much!
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sqlalchemy/e9b04c7e-d402-4025-81c2-62a0f73e2dcbn%40googlegroups.com.