Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: __init__() got an unexpected keyword argument error

I have 6 tables in an sqlite database and I'm trying to add a new row in one of the tables using sqlalchemy. Here is my tables:

class DSource(Base):
    __tablename__ = 'dsource'

    source_id = Column(Integer, primary_key=True)
    subjects= relationship("Subject") 

class Subject(Base):
    __tablename__ = 'subject'

    subject_id = Column(Integer, primary_key=True)
    source_id=Column(Integer, ForeignKey("dsource.source_id"),nullable=False)
    sequences= relationship("Sequence")

class Sequence(Base):
     __tablename__ = 'sequence'

     sequence_id = Column(Integer, primary_key=True)
     subject_id=Column(Integer, ForeignKey("subject.subject_id"),nullable=False)

Here is the code I'm using to add a new sequence to the table:

engine = create_engine('sqlite:////Desktop/emotion_data/test.db',echo=True)
Session = sessionmaker(bind=engine)
session = Session()

new_sequence=Sequence(sequence_id=0,subject_id=1)
session.add(new_sequence)
session.commit()

But I'm getting this error:

TypeError: __init__() got an unexpected keyword argument 'subject_id'

I have no idea what does that mean, I already have imported a "subject" instance with "subject_id=0" to the table, so the subject_id=0 is already in the database.

Can anybody please help me?

like image 320
user2308191 Avatar asked Dec 15 '25 08:12

user2308191


1 Answers

Your code looks fine, if the exception only raises when your split your source to different files. There must be something like name conflict, Sequence class may not be what you expected, you can check this by prepending those lines before the init process.

print Sequence.__module__
print type(Sequence)
like image 170
piglei Avatar answered Dec 16 '25 21:12

piglei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!