Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sqlite uuid relationships #1128

Closed
dvlsg opened this issue Nov 1, 2017 · 4 comments
Closed

Sqlite uuid relationships #1128

dvlsg opened this issue Nov 1, 2017 · 4 comments

Comments

@dvlsg
Copy link
Contributor

dvlsg commented Nov 1, 2017

When using @PrimaryKeyGenerated('uuid') for entities with relationships (@ManyToOne() and @OneToMany(), specifically), I'm getting errors similar to the following:

QueryFailedError: SQLITE_ERROR: foreign key mismatch - "temporary_child" referencing "p
arent"

Here's an example which should reproduce the issue, I think. I'm not 100% sure I'm doing it correctly, because I'm new to typeorm, but swapping from @PrimaryKeyGenerated('uuid') to just @PrimaryKeyGenerated() and a number instead of a string seems to work.

import {
  Entity,
  PrimaryGeneratedColumn,
  OneToMany,
  ManyToOne,
  createConnection,
} from 'typeorm'

@Entity()
class Parent {
  @PrimaryGeneratedColumn('uuid') id?: string

  @OneToMany(type => Child, child => child.parent)
  children: Child[]
}

@Entity()
class Child {
  @PrimaryGeneratedColumn('uuid') id?: string

  @ManyToOne(type => Parent, parent => parent.children)
  parent: Parent
}

createConnection({
  type: 'sqlite',
  database: 'test.db',
  entities: [Parent, Child],
  synchronize: true,
}).then(conn => {
  console.log('ready!')
})

Here are my versions:

  1. typeorm: 0.1.1
  2. sqlite3: 3.1.13

Haven't tried it with anything other than sqlite just yet. Might try in postgres in a second, if I get a chance.

@dvlsg
Copy link
Contributor Author

dvlsg commented Nov 1, 2017

Just gave it a shot with postgres (v10, I think), seems to work fine.

@maddindeiss
Copy link

Hi,
I'm getting a similar error here with the cordova driver.

sqlite3_prepare_v2 failure: foreign key mismatch - "temporary_project" referencing "user"

Example code:

@Entity('project')
export class Project {
  @PrimaryGeneratedColumn('uuid')
  public id: string;

  @ManyToOne(type => User)
  public user: User
}

@Entity('user')
export class User {
  @PrimaryGeneratedColumn('uuid')
  public id: string;

  @Column({ name: 'name', type: 'varchar', length: 255 })
  public name: string
}

createConnection({
  type: 'cordova',
  location: 'default',
  database: 'test',
  entities: [
    Project, 
    User
  ],
  synchronize: true,
}).then(conn => {
  console.log('ready!')
})

I'm using:

  • ionic 3.8.0
  • typeorm: 0.1.2
  • cordova-sqlite-storage: 2.1.1

@benedictkhoo
Copy link

Hi,

I too am getting the same error using sqlite.

I enabled logging and I noticed that my entities with @PrimaryGeneratedColumn('uuid') does not generate a SQL statement using CREATE TABLE with PRIMARY KEY(id).

With @PrimaryColumn('uuid'):

@Entity()
export class User {
  @PrimaryColumn('uuid')
  id: string;
}
CREATE TABLE "user" ("id" varchar NOT NULL, PRIMARY KEY(id))

With @PrimaryGeneratedColumn('uuid'):

@Entity()
export class Message {
  @PrimaryGeneratedColumn('uuid')
  id: string;
}
CREATE TABLE "message" ("id" varchar NOT NULL)

I'm using:

  • typeorm: 0.1.1
  • sqlite3: 3.1.13

@pleerock
Copy link
Member

This issue is fixed and released in 0.1.6.

pleerock pushed a commit that referenced this issue Nov 22, 2017
* master:
  fixes #1128 #1161
  fixed broken issue
  Failing test for issue #1034

# Conflicts:
#	package.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants