Mar-31-2018, 01:22 PM
Newbie with Python. Just downloaded latest ver. 3.6.5. Using sqlite3 (that is loaded with Python) for small work database. Creating one-to-many relationship between 2 tables. Created foreign key constraint but, when I inserted what should be a 'bad' record to my child table, Python did not return an error. So the foreign key is not being enforced. Don't know how to fix this? Here is a simplified version of my commands. With only one record inserted in the transformers table (which will have id = 1), I shouldn't be able to insert a record into the windings table with transformer_id = 4
created the following tables with these 2 commands:
c.execute('CREATE TABLE transformers(id INTEGER PRIMARY KEY, weight INTEGER)')
c.execute('CREATE TABLE windings(id INTEGER PRIMARY KEY, transformer_id INTEGER NOT NULL, \
FOREIGN KEY(transformer_id) REFERENCES transformers(id))')
inserted values into the tables with these 2 commands:
c.execute('INSERT INTO transformers(name) VALUES(4523)')
c.execute('INSERT INTO windings(transformer_id) VALUES(4)')
New to Python and this forum, so I apologize in advance if my post doesn't meet normal format for posts.
created the following tables with these 2 commands:
c.execute('CREATE TABLE transformers(id INTEGER PRIMARY KEY, weight INTEGER)')
c.execute('CREATE TABLE windings(id INTEGER PRIMARY KEY, transformer_id INTEGER NOT NULL, \
FOREIGN KEY(transformer_id) REFERENCES transformers(id))')
inserted values into the tables with these 2 commands:
c.execute('INSERT INTO transformers(name) VALUES(4523)')
c.execute('INSERT INTO windings(transformer_id) VALUES(4)')
New to Python and this forum, so I apologize in advance if my post doesn't meet normal format for posts.