Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. PyQt: Basic foreign key misbehaving?
QtWS25 Last Chance

PyQt: Basic foreign key misbehaving?

Scheduled Pinned Locked Moved Solved General and Desktop
pyqtsqliteforeign key
2 Posts 1 Posters 389 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    judethedude
    wrote on 18 Jul 2022, 15:29 last edited by judethedude
    #1

    Hey everyone, I'm building some tables and encountered something weird. I'm building my two tables with this code (triple quotes isn't formatting correctly):

    create_table_query = QSqlQuery()
    create_table_query.exec(
            """
            CREATE TABLE IF NOT EXISTS doc (
                doc_id INTEGER PRIMARY KEY,
                doc_name TEXT,
                doc_number TEXT,
                doc_address TEXT,
                doc_notes TEXT
                )
            """
        )
    
    create_table_query.exec(
            """
            CREATE TABLE IF NOT EXISTS patient_profile (
                patient_id INTEGER PRIMARY KEY,
                doc_id INTEGER NOT NULL,
                doc_secondary_id INTEGER NOT NULL,
                name_title TEXT,
                name_fname TEXT,
                FOREIGN KEY (doc_id) REFERENCES doc (doc_id)          
                )
            """
        )
    

    However, in my patient_profile table, if I rename doc_id to anything that doesn't match the name of the primary id in the doc table, the foreign key relationship ceases to exist. For example, I'd like patient_profile to look like this:

        create_table_query.exec(
            """
            CREATE TABLE IF NOT EXISTS patient_profile (
                patient_id INTEGER PRIMARY KEY,
                doc_primary_id INTEGER NOT NULL,
                doc_secondary_id INTEGER NOT NULL,
                name_title TEXT,
                name_fname TEXT,
                FOREIGN KEY (doc_primary_id) REFERENCES doc (doc_id)          
                )
            """
        )
    

    But doc_primary_id doesn't show up as a foreign key in my sqlite browser.
    Am I missing something obvious here?
    Thanks for your time, Jude.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      judethedude
      wrote on 18 Jul 2022, 15:50 last edited by judethedude
      #2

      Solved, sorry to bother everyone.
      I needed to designate my patient_id as UNIQUE

      1 Reply Last reply
      0

      1/2

      18 Jul 2022, 15:29

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved