sqlalchemy : Column must be constructed with a non-blank name

Viewed 617

I try to insert new records into a table on amazon redshift . Basically i download rows from a google spreadsheet and try to push as a Dataframe into a table.

from sqlalchemy import create_engine
engine = create_engine('postgresql://redshift_user:password,GB8@redshift-url:5439/datawarehouse')
connection = engine.raw_connection()

and then i excute

df.to_sql(name='test',con=engine, if_exists='append', index=False)

but it dosen't work and it raises the exception

sqlalchemy.exc.ArgumentError: Column must be constructed with a non-blank name or assign a non-blank .name before adding to a Table.

i checked some posts and tried their solutions but still not working

2 Answers

Try this:

import SQLAlchemy sqla

db_uri = ''
db = sqla.create_engine('postgresql://file.db')


class Table(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)

    def __repr__(self):
        return f'USER[{self.id}, {self.name]'
db.create_all()
user1 = Table(username='User1')
db.session.add(user1)
db.session.commit()

WARNING YOU WILL HAVE TO DEBUG THIS... I only worked with Flask + SQLAlchemy.

thx for the help.

Turn out that i had an empty column in my dataframe that i didn't notice in the beginning that caused the error . Once i fixed the import from google sheets , the parsing to the database also worked

Related