Hi All!

Crisis reverted, turned out my database column didn't have the 
autoincerement flag.
Sorry for the topic.

Martin Parrag a következőt írta (2023. szeptember 21., csütörtök, 16:53:14 
UTC+2):

> 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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/ce862ff5-be17-4886-ad8e-49ad51a9dc05n%40googlegroups.com.

Reply via email to