synw / sqlcool

Easy and reactive Sqlite for Flutter
MIT License
163 stars 28 forks source link

Doubt about Primary Key #22

Closed ederluca closed 3 years ago

ederluca commented 4 years ago

Imagine the following table:

final calendario = DbTable ("Calendario")    ..integer ("CalID", unique: true, nullable: false)    ..varchar ("CalTipo", unique: true, nullable: false)    ..integer ("CalEntDia", nullable: false)    ..integer ("CalEntMes", nullable: false)    ..integer ("CalEntAno", nullable: false)    ..varchar ("CalDescricao", nullable: false)    ..integer ("CalVencDia", nullable: false)    ..integer ("CalVencMes", nullable: false)    ..integer ("CalVencAno", nullable: false)    ..varchar ("CalURL", nullable: false)    ..integer ("CalOecID", nullable: false);

Question when defining the column CalID and CalTipo as unique = true does he understand that it is a composite primary key? If not, how do I define this situation?

synw commented 4 years ago

unique only checks the uniqueness per column.

For a composite uniqueness check you need ùniqueTogether:

..uniqueTogether("CalID", "CalTipo")

I think this is not documented, the doc is not up to date and should be refreshed one of these days

[Edit] : by the way you don't need nullable: false: all is non nullable by default

ederluca commented 4 years ago

Thank you very much for returning

ederluca commented 4 years ago

I performed the test with the command approach that sent me but on the debugger console shows the following problem:

I / flutter (26771): id INTEGER PRIMARY KEY, I / flutter (26771): CalID INTEGER NOT NULL, I / flutter (26771): CalType VARCHAR NOT NULL, I / flutter (26771): CalEntDia INTEGER NOT NULL, I / flutter (26771): CalEntMes INTEGER NOT NULL, I / flutter (26771): INTEGER NOT NULL CALENT, I / flutter (26771): CalDescription VARCHAR NOT NULL, I / flutter (26771): CalVencDia INTEGER NOT NULL, I / flutter (26771): CalVencMes INTEGER NOT NULL, I / flutter (26771): CalVencAno INTEGER NOT NULL, I / flutter (26771): CalURL VARCHAR NOT NULL, I / flutter (26771): CalOecID INTEGER NOT NULL I / flutter (26771):) in 6 ms E / SQLiteLog (26771): (1) near "UNIQUE": syntax error

The table syntax looks like this: final calendario = DbTable ("Calendario")   ..integer ("CalID", nullable: false)   ..varchar ("CalTipo", nullable: false)   ..integer ("CalEntDia", nullable: false)   ..integer ("CalEntMes", nullable: false)   ..integer ("CalEntAno", nullable: false)   ..varchar ("CalDescricao", nullable: false)   ..integer ("CalVencDia", nullable: false)   ..integer ("CalVencMes", nullable: false)   ..integer ("CalVencAno", nullable: false)   ..varchar ("CalURL", nullable: false)   ..integer ("CalOecID", nullable: false)   ..uniqueTogether ("CalID", "CalTipo");

Take the contents of calendar.queries where you generated the following: I / flutter (26771): [CREATE TABLE IF NOT EXISTS Calendario ( I / flutter (26771): id INTEGER PRIMARY KEY, I / flutter (26771): CalID INTEGER NOT NULL, I / flutter (26771): CalType VARCHAR NOT NULL, I / flutter (26771): CalEntDia INTEGER NOT NULL, I / flutter (26771): CalEntMes INTEGER NOT NULL, I / flutter (26771): INTEGER NOT NULL CALENT, I / flutter (26771): CalDescription VARCHAR NOT NULL, I / flutter (26771): CalVencDia INTEGER NOT NULL, I / flutter (26771): CalVencMes INTEGER NOT NULL, I / flutter (26771): CalVencAno INTEGER NOT NULL, I / flutter (26771): CalURL VARCHAR NOT NULL, I / flutter (26771): CalOecID INTEGER NOT NULL I / flutter (26771):), UNIQUE (CalID, CalType)]

I took the create table and played it on a sqlite test bench looking like this: CREATE TABLE IF NOT EXISTS Calendario ( id INTEGER PRIMARY KEY, CalID INTEGER NOT NULL, CalType VARCHAR NOT NULL, CalEntDia INTEGER NOT NULL, CalEntMes INTEGER NOT NULL, CalEntOno INTEGER NOT NULL, CalDescription VARCHAR NOT NULL, CalVencDia INTEGER NOT NULL, CalVencMes INTEGER NOT NULL, CALVENT INTEGER NOT NULL, CalURL VARCHAR NOT NULL, CalOecID INTEGER NOT NULL ), UNIQUE (CalID, CalType) Generating the error: SQL Error [1]: [SQLITE_ERROR] SQL error or missing database (near ",": syntax error)

However, if you change create as follows: CREATE TABLE IF NOT EXISTS Calendario ( id INTEGER PRIMARY KEY, CalID INTEGER NOT NULL, CalType VARCHAR NOT NULL, CalEntDia INTEGER NOT NULL, CalEntMes INTEGER NOT NULL, CalEntOno INTEGER NOT NULL, CalDescription VARCHAR NOT NULL, CalVencDia INTEGER NOT NULL, CalVencMes INTEGER NOT NULL, CALVENT INTEGER NOT NULL, CalURL VARCHAR NOT NULL, CalOecID INTEGER NOT NULL, UNIQUE (CalID, CalTipo) )

The Command is successfully executed.

synw commented 4 years ago

Thanks, I'll try to fix this asap

ederluca commented 4 years ago

No problem

synw commented 4 years ago

@ederluca : did you test with the latest version? uniqueTogether was fixed not so long ago: https://github.com/synw/sqlcool/commit/d0a23c2937293a3e08ab17ec97b9d3737c784e5c

[Edit] : it was supposed to be fixed in 4.3.0