niashvili
niashvili

Reputation: 21

SQLAlchemy objects don't recognize new columns after DB schema was changed

I have a local DB set up for which I create object-relational mapping with SQLAlchemy objects.

Everything was fine until I changed the schema of the DB - including adding a new column to one of the tables. Now I keep seeing:

sqlalchemy.exc.NoReferencedColumnError: Could not initialize target column for ForeignKey 'ModelFit.id' on table 'ModelPrice': table 'ModelFit' has no column named 'id'

where 'id' is the SQLALchemy Column object of ModelFit table's "Id" column.

Straight SQL queries on the new DB execute fine, the only problem is initializing this mapping.

I saw a similar question from someone saying they figured it out by "removing the .db file from the project and ran it again", but I don't have any such file. I don't even use flask or anything to create the DB, did it straight in local DB using SQL.

Any help or insight on what is happening here? Or would more info be helpful?

Upvotes: 0

Views: 1129

Answers (1)

niashvili
niashvili

Reputation: 21

Figured it out eventually - seems like the order of columns had to be the same. Running ALTER TABLE on the schema to add an "Id" column added it to the right. My SQLAlchemy class had "id" column listed as the first one.

I can't confirm the reason why because I haven't looked at the part of the docs that would confirm this, but anecdotally, this was the case.

So basically had to delete the table and re-make it with the desired ordering.

Upvotes: 0

Related Questions