Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declarative style for ObsRawNativeFlags and ObsRawPCICFlags #95

Open
rod-glover opened this issue May 25, 2021 · 2 comments
Open

Declarative style for ObsRawNativeFlags and ObsRawPCICFlags #95

rod-glover opened this issue May 25, 2021 · 2 comments

Comments

@rod-glover
Copy link
Contributor

We'd like declare ObsRawNativeFlags and ObsRawPCICFlags in declarative base (class) style, as below.

If we do so withprimary_key=True, some tests error out (specifically, but I suspect not only, tests/alembic_migrations/versions/v_7a3b247c577b_add_varsperhistory_native_matview/test_matview.py::test_vars_content).

If we omit primary_key, then SQLAlchemy objects that it cannot determine primary key columns for the table, which is hardly surprising.

Figure out what is going on here and fix it.

Question: Why is it OK to declare a table without primary keys using a functional X = Table(...) declaration, and not using class X?

class ObsRawNativeFlags(Base):
    """Association table for Obs *--* NativeFLag"""
    __tablename__ = "obs_raw_native_flags"

    obs_raw_id = Column(
        BigInteger,
        ForeignKey("obs_raw.obs_raw_id"),
        # primary_key=True,
    )
    native_flag_id = Column(
        Integer,
        ForeignKey("meta_native_flag.native_flag_id"),
        # primary_key=True,
    )

    # Constraints
    obs_raw_native_flag_unique = UniqueConstraint(
        'obs_raw_id', 'native_flag_id', name='obs_raw_native_flag_unique'
    )

Index("flag_index", "obs_raw_id")


class ObsRawPCICFlags(Base):
    """Association table for Obs *--* PCICFLag"""
    __tablename__ = 'obs_raw_pcic_flags'

    obs_raw_id = Column(
        BigInteger,
        ForeignKey('obs_raw.obs_raw_id'),
        # primary_key=True,
    )
    pcic_flag_id = Column(
        Integer,
        ForeignKey('meta_pcic_flag.pcic_flag_id'),
        # primary_key=True,
    )

    # Constraints
    obs_raw_pcic_flag_unique = UniqueConstraint(
        'obs_raw_id', 'pcic_flag_id', name='obs_raw_pcic_flag_unique'
    )


Index('pcic_flag_index', 'obs_raw_id')
@rod-glover
Copy link
Contributor Author

A possible aid: An SO reply mentions that:

If the table already exist you can reference to the fields directly:

class Humans(Base):
    __table__ = people_table
    id = people_table.c.PeopleID
    name = people_table.c.PeopleName

In the absence of a better solution, this may give us a way to at least give the effect of a declarative style, whilst retaining the Table definitions. Benefit: Code completion will work better. Nada más.

@rod-glover
Copy link
Contributor Author

It may also be possible to avoid the problem by creating the FK constraint outside of the table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant