0

I want to relate two tables in my database. I use sqlalchemy and mariadb for this project.

It works well when the column description in table description is set String with the value up to 700.

class Edition(Base):
    __tablename__= "edition"

    idEdition = Column(Integer, primary_key=True)
    description = relationship("Description", back_populates="edition")

class Description(Base):
    __tablename__ = "description"

    idDescription = Column(Integer, primary_key=True)
    edition_id = Column(Integer, ForeignKey('edition.idEdition'), nullable=False)
    description = Column(String(700), nullable=False)
    edition = relationship("Edition", back_populates="description")
    UniqueConstraint(edition_id, description)

But I need to store longer descriptions. If I try description = Column(String(800), nullable=False) there is an error:

sqlalchemy.exc.DatabaseError: (mysql.connector.errors.DatabaseError) 1005 (HY000): Can't create table `bestsellers`.`description` (errno: 150 "Foreign key constraint is incorrectly formed")

The same with the description = Column(LONGTEXT, nullable=False)

I assume there is a problem with types, but for the moment I can't find where...

EDIT Following Gord Thompson suggestion I change the Description class:

class Description(Base):
    __tablename__ = "description"

    idDescription = Column(Integer, primary_key=True)
    edition_id = Column(Integer, ForeignKey('edition.idEdition'), nullable=False)
    description = Column(LONGTEXT, nullable=False)
    description_short = Column(String(750), nullable=False)
    edition = relationship("Edition", back_populates="description")
    UniqueConstraint(edition_id, description_short)

and it seems to be working. However, I wonder if mysql:

CREATE TABLE description (idDescription INTEGER NOT NULL AUTO_INCREMENT,

edition_id INTEGER NOT NULL, description LONGTEXT NOT NULL, PRIMARY KEY 

(`idDescription`), UNIQUE (edition_id, description(750)), FOREIGN KEY(edition_id) 

REFERENCES edition (`idEdition`));

could be directly translated into python sqlalchemy? I would prefer not to add new column. It would be great to just limit existing column for the purpose of UniqueConstraint.

Maciek
  • 33
  • 1
  • 8

0 Answers0