Home > Net >  Postgres SQLAlchemy: Additional arguments should be named <dialectname>_<argument>, got
Postgres SQLAlchemy: Additional arguments should be named <dialectname>_<argument>, got

Time:03-07

I am using postgres database and sqlalchemy in my python code, I created schemas and models and I want to use ForeignKey, but I got this error message:

Additional arguments should be named <dialectname>_<argument>, got 'ForeignKey'

my code:

class table1(Base):
    __tablename__ = 'table1'
    first_id = Column(
        Integer, unique=True, nullable=False, primary_key=True, autoincrement=True
    )


class table2(Base):
    __tablename__ = 'table2'
    second_id = Column(
        Integer,
        nullable=False,
        primary_key=True,
    )
    second = Column(Integer, nullable=False, primary_key=True, ForeignKey=('table1.first_id'))

Could anyone help me with this? Thanks.

CodePudding user response:

You need to import ForeignKey from sqlalchemy and create it as positional argument to Column.

from sqlalchemy import ForeignKey

class table1(Base):
    __tablename__ = 'table1'
    first_id = Column(
        Integer, unique=True, nullable=False, primary_key=True, autoincrement=True
    )


class table2(Base):
    __tablename__ = 'table2'
    second_id = Column(
        Integer,
        nullable=False,
        primary_key=True,
    )
    second = Column(Integer, ForeignKey('table1.first_id'), nullable=False, primary_key=True)

Documentation

ForeignKey

Column params

  • Related