Tuesday, 13 November 2018

SQLAlchemy Postgres override the unique for ForeignKey

Have a (Many-to-Many) helper table with a column that contains duplicate entries which I want to reference with a ForeignKey in Many-to-One Table. Using SQLAlchemy is there a way to override the column with dupe values so I can ignore the duplicate'ness and use it's unique values as foreignkey? Using like unique=False or something?

import SQLAlchemy
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm import sessionmaker, session
from sqlalchemy.types import Integer, String
from sqlalchemy import types, create_engine, MetaData, Table, Column, ForeignKey, ForeignKeyConstraint, UniqueConstraint, CheckConstraint
from sqlalchemy.ext.declarative import declarative_base

postgres_dbpostgres  = {'drivername': 'postgresql',
           'username': '***',
           'password': '***',
           'host': '***',
           'port': 5432,
           'database': '***'}

PSQLengine = create_engine(URL(**postgres_dbpostgres), echo=True)

metadata = MetaData(PSQLengine)
Base = declarative_base()

class Helper(Base):
    __tablename__ = 'helper'
    _id = Column(Integer(), primary_key=True)
    value_a = Column(String(), ForeignKey
    value_with_dupes = Column(String(), unique=False)


class ReffingTable(Base):
    __tablename__ = 'reffingtable'
    _id = column(Integer(), primary_key=)
    value_refing_dupes = Column(String(), ForeignKey('helper.value_with_dupes')

Base.metadata.create_all(PSQLengine)



from SQLAlchemy Postgres override the unique for ForeignKey

No comments:

Post a Comment