0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I have imported it, and it is defined in Column
You are using the keyword argument "ForeignKey" to Column instead of declaring an instance of ForeignKey. Please look at the second = Column... line in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.