-
Notifications
You must be signed in to change notification settings - Fork 5
Description
When using either of the cascade
or order_by
fields on a mapped relationship
inside a SQLAlchemy ORM class, Atlas does not recognise these changes, and the result of atlas migrate diff
is no migration scripts being produced.
For example, assume we have the following, where DefaultSchemaModel
is just a subclass of Base
:
class Message(DefaultSchemaModel):
__tablename__ = 'messages'
transcript_id: Mapped[int] = mapped_column(
ForeignKey('transcripts.id'),
nullable=False,
comment='The ID of the conversation this message belongs to',
)
content: Mapped[str] = mapped_column(
Text,
nullable=False,
comment='The content of the message',
)
transcript: Mapped['Transcript'] = relationship(
back_populates='messages',
lazy='selectin',
)
class Transcript(DefaultSchemaModel):
__tablename__ = 'transcripts'
messages: Mapped[list['Message']] = relationship(
back_populates='transcript',
lazy='selectin',
)
assistant: Mapped['Assistant'] = relationship(
back_populates='transcripts',
lazy='selectin',
)
Then, we execute atlas migrate diff
, and our migrations are generated. Next, I update Transcript.messages
with the following properties:
messages: Mapped[list['Message']] = relationship(
back_populates='transcript',
lazy='selectin',
+ cascade='all, delete-orphan',
+ order_by='desc(Message.created_at)',
)
This is a logical change, as a transcript should destroy all of its associated messages when it is deleted. However, running atlas migrate diff
now does not produce any output migration file.
I ended up writing the following migration manually, which is what I would expect Atlas to output:
-- Add cascade delete and ordering for transcript-message relationship
ALTER TABLE messages
DROP CONSTRAINT messages_transcript_id_fkey,
ADD CONSTRAINT messages_transcript_id_fkey FOREIGN KEY (transcript_id) REFERENCES transcripts (id) ON DELETE CASCADE;
-- Add index for ordering messages by created_at desc
CREATE INDEX idx_messages_created_at ON messages (created_at DESC);
Perhaps I am missing something, let me know!