sqlalchemy-redshift: Redshift does not support sequences

The dialect needs to specify that sequences are not supported. Otherwise sqlalchemy tries to reference a nonexistent sequence and you get an error such as the following: (sqlalchemy.exc.ProgrammingError) (psycopg2.ProgrammingError) relation "tablename_model_id_seq" does not exist [SQL: 'select nextval(\'"tablename_model_id_seq"\')'] [SQL: u'INSERT INTO tablename (model_id, model_name) VALUES (%(model_id)s, %(model_name)s,)']

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Comments: 19 (6 by maintainers)

Commits related to this issue

Most upvoted comments

I think I got it working by setting

RedshiftDialect.supports_sequences = False
RedshiftDialect.preexecute_autoincrement_sequences = False
**RedshiftDialect.postfetch_lastrowid = True**

I’m still debugging things, but so far there’s no been any primary key related errors.

Any updates on this?

Hey nwbvt, thank you so much! It kind of works. The ‘(psycopg2.ProgrammingError) relation “tablename_model_id_seq” does not exist’ error is solved by setting those two variables to False, but now I’m getting the following:

sqlalchemy.orm.exc.FlushError: Instance [ENTITY] has a NULL identity key. 
If this is an auto-generated value, check that the database table allows generation of new primary key values, and that the mapped Column object is configured to expect these generated values. 
Ensure also that this flush() is not occurring at an inappropriate time, such as within a load() event.

I guess there should be a

SELECT MAX([id_column])
FROM [entity_table]

immediately following the INSERT INTO [entity_table] statement.

I’m kind of new to SqlAlchemy, do you have any idea where/what should I modify in sqlalchemy_redshift to add this?

Thanks again,

Mario