Open JabberWocky-22 opened 3 weeks ago
Hi,
It seems like a bug, but I'm proposing to close as won't fix.
The reason is that they table definition that you have are highly unlikely, since it doesn't make much sense adding a fk on an auto-incrementing column. Since postgresql does not tell you if a column is a serial when reflecting a table, alembic has some heuristic to figure out when it is and whet it isn't. The presence of a fk on the column likely makes alembic think that the column is not a serial.
Thoughts @zzzeek ?
Actually, it can happen on a table with serial column and being referenced by another table(doesn't matter adding foreign key on which column), just like:
CREATE SCHEMA test;
CREATE TABLE test.account1(short_id SERIAL PRIMARY KEY, id uuid unique);
CREATE TABLE test.account2(short_id SERIAL PRIMARY KEY, id uuid unique REFERENCES test.account1(id));
ALTER TABLE test.account1 ADD FOREIGN KEY (id) REFERENCES test.account2(id); -- not necessary
Besides two tables reference each other just make it easier to reproduce, it's not necessary.
Looks like when reflecting table, the referenced table will be reflected too, since the default value of resolve_fks
is True.
And the event hook won't take effect for the referenced table.
another similar issue: https://github.com/sqlalchemy/alembic/issues/1454
Describe the bug There is a probability that serial column was not detected. This can happen when the table is referenced by another table.
Expected behavior The result of autogererate should't contain server default for column id.
To Reproduce
Error It will produce two outputs and the first one is incorrect.
Versions.
Additional context If table is not fererenced by other table, the result is always correct. If I make both tables reference each other, the first table in autogenerate is alway incorrect.
I guess the listen hook migth fail with foreign keys, not sure since havn't dig into code much.
Have a nice day!