0

I am trying to load a list into my SQLite DB using SQLAlchemy. Main code below:

res = zip(list_of_first_names,list_of_last_names,list_of_ids,list_of_emails)

def db_commit():
for i in res:
    temp_dict = {'first_name': i[0], 'last_name': i[1], 'user_id': i[2], 'email': i[3]}
    # Example Output: {'first_name': 'John', 'last_name': 'Doe', 'user_id': '638283', 'email': 'john.doe@gmail.com'
    employee = Employees(**temp_dict)
    db.session.add(employee)
    db.session.commit()

I modeled my db_commit function after the answer that was provided in this question: generalised insert into sqlalchemy using dictionary , but my code keeps giving me this error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/mnt/c/Users/user/Documents/VSCode/Projects/my_project/CMS/Flask/Flask_App/foo.py", line 878, in db_commit
# print(temp_dict)
TypeError: __init__() takes 1 positional argument but 2 were given

By the way, the Employee class is defined as:

class Employees(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    first_name = db.Column(db.String(20), nullable = False)
    last_name = db.Column(db.String(20), nullable = False)
    user_id = db.Column(db.Integer, unique = True, nullable = False)
    email = db.Column(db.String(40), unique = True, nullable = False)

    def __repr__(self):
        return f"User('{self.first_name}','{self.last_name}','{self.shopify_id}', '{self.email}')"

Any idea on what I am doing wrong from the verified answer in the answer linked?

  • I can only reproduce this with `Employee(temp_dict)`. Is the code in the question the code that is actually running? – snakecharmerb May 14 '20 at 08:38
  • Hi @snakecharmerb. Thanks for commenting. After I posted I realized I had some issues with the actual code that was being ran being different and I also had an issue where my project was structured in a way that would not allow my code to run. I moved the file I was running outside of that directory and then it worked! So it was not an issue with my function, it was a problem with my package structure. – JTorres-310 May 14 '20 at 14:19

0 Answers0