0

I'm trying to generate some data in a migration to put in a table with quite a few columns. The migration in itself is part of several migration scripts, with the first one generating the table:

#abcd123_initial_migration.py


def upgrade():
    op.create_table(
        "mytable",
        sa.Column(...,...,...)
        #and so on
    )

This is working fine. In the next migration however I have a need to read this mytable from the connection, like so:

#dfcd32_next_migration.py


def upgrade():

     #compute inserts:
     for i in range(0, 100000):
        my_big_fat_obj = { ... }
        sql_statements.append(my_big_fat_row)

     meta = MetaData().reflect(bind=op.get_bind(), schema="myschema", only=("mytable",))
     op.bulk_insert(Table("mytable", meta), sql_statements) <-- fails

File ".../lib/python3.8/site-packages/sqlalchemy/sql/schema.py", line 452, in _new
    schema = metadata.schema`
AttributeError: 'NoneType' object has no attribute 'schema'

If i put a breakpoint and investigate, it seems that the first migration hasnt committed its changes yet, because there are no tables, views or indexes in the database yet.

How can I make sure "mytable" exists on the reflected metadata without having to repeat the table definition in this migration file?

EDIT: It seems that calling meta = MetaData().reflect() is not the way to go about it. Instead, call

meta = MetaData()
meta.reflect(..).

otherwise the call to reflect will replace the meta reference with a "None"

enrm
  • 645
  • 1
  • 8
  • 22
  • Does this answer your question? [Why do I get AttributeError: 'NoneType' object has no attribute 'something'?](https://stackoverflow.com/questions/8949252/why-do-i-get-attributeerror-nonetype-object-has-no-attribute-something) – Ulrich Eckhardt Jul 10 '23 at 12:40

0 Answers0