'Godot SQLite Foreign Key
Could someone please show me how to create a foreign key relationship between two tables in Godot. I can not figure based on the current documentation.
extends Node2D
const SQLite = preload("res://addons/godot-sqlite/bin/gdsqlite.gdns")
var save_path = "user://data.sqlite"
var db
func _ready():
db = SQLite.new()
db.path = save_path
db.foreign_keys = true
db.open_db()
db.create_table("dog_names", {
"id": {"data_type": "int", "primary_key": true, "auto_increment": true},
"first_name": {"data_type": "text"},
"last_name": {"data_type": "text"},
})
db.create_table("owners", {
"id": {"data_type": "int", "primary_key": true, "auto_increment": true},
"name": {"data_type": "text"}
})
db.query("SELECT * FROM dog_names;")
print(db.query_result[0]["first_name"])
Essentially I just want to add a many-to-one relationship between table owner and table dog. One owner can have many dogs but a dog can only have one owner.
Thanks
Solution 1:[1]
So is this the best/only way to get information from the foreign_key relationship?
db.query("SELECT owner FROM dog_names;")
print(db.query_result)
print(db.query_result[0])
var o = db.query_result[0]["owner"]
print(o)
var o_str = "SELECT name FROM owners WHERE id = {int};"
var o_form = o_str.format({"int": o})
db.query(o_form)
print(db.query_result)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tony Dean |
