I have a SwiftData model that has a few many-to-many relationships that require an intersection table.
My question relates to how the intersection tables are automatically named in sqlite.
My model has one main table called LogEntry. Each LogEntry can be assigned zero or more ObjectColor, ObjectDecoration, and FaunalModification. Obviously, each color, decoration, and modification could be assigned to multiple log entries.
Relevant attributes shown below:
@Model
final class LogEntry {
...
var objectDecorations: [ObjectDecoration]?
var objectColors: [ObjectColor]?
var faunalModifications: [FaunalModification]?
...
@Model
final class ObjectColor: Lookup {
...
@Relationship(inverse: LogEntry.objectColors)
var logentries: [LogEntry]?
...
@Model
final class ObjectDecoration: Lookup {
...
@Relationship(inverse: LogEntry.objectDecorations)
var logentries: [LogEntry]?
....
@Model
final class FaunalModification: Lookup {
...
@Relationship(inverse: LogEntry.faunalModifications)
var logentries: [LogEntry]?
...
What I noticed when viewing the generated sqlite table names, I get tables for each class, plus an intersection table between LogEntry and each of the three lookup tables.
My question is around the name of the intersection tables that are generated:
ObjectColor/LogEntry: Z_11OBJECTCOLORS
ObjectDecoration/LogEntry: Z_11OBJECTDECORATIONS
FaunalModification/LogEntry: Z_5LOGENTRIES
That last one surprised me as it’s inconsistent with the other two, although the models are defined in a very similar way.
After some experimentation, I think that the logic used to come up with the tables names is alphabetic: essentially the intersection table is named after whichever table comes second alphabetically. Now that I am writing it down, I’m not 100% sure it’s based on the class names or if it’s based on the attribute names but I think it’s the class names.
I know this doesn’t really matter in terms of how the database functions, but is there any way to control or influence the names of these entities? I suppose I could manually define them as Model classes, as I would do if, for example, there were additional attributes needed in the intersection table. Is there a better/preferred way of managing this? I’m pretty new to Swift.