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?