cotton
cotton copied to clipboard
@Column requires type
The docs for models say "Cotton is smart enough to determine the data type of that particular column using TypeScript types. However, you can still customize your column types by passing the type option like below." Then it shows this:
@Column({ type: DataType.String })
email!: string;
When I try this:
@Column()
name!: string;
without passing a type to @Column
I get the error "Column '${propertyKey}' must have a type!".
If I do pass the type to @Column
then it works.
Should I really be able to just use @Column()
?
Hi, thanks for using the library! 😃
I'm not sure what that happens. There's actually a test case for it and it passed.
https://github.com/rahmanfadhil/cotton/blob/4ddc9561f4874643ec201be2b63ab9f31602502d/src/model_test.ts#L60-L99
Can you give more detail about your project, like which the Deno and Cotton version that you're using?
I am using Deno 1.5.4 with the latest version of Cotton imported with https://deno.land/x/cotton/mod.ts. Here is my simple test case:
import {
Column,
connect,
DataType,
Model,
Primary,
} from "https://deno.land/x/cotton/mod.ts";
const db = await connect({ type: "sqlite", database: "pets.db" });
const manager = db.getManager();
@Model("dogs")
class Dog {
@Primary()
id!: number;
// Omitting the type here gives the error
// "Column '${propertyKey}' must have a type!"
@Column({ type: DataType.String })
name!: string;
@Column({ type: DataType.String })
breed!: string;
}
await db.query("delete from dogs");
const initialDogs = [
{ name: "Maisey", breed: "Treeing Walker Coonhound" },
{ name: "Ramsay", breed: "Native American Indian Dog" },
{ name: "Oscar", breed: "German Shorthaired Pointer" },
{ name: "Comet", breed: "Whippet" },
];
for (const { name, breed } of initialDogs) {
const dog = new Dog();
dog.name = name;
dog.breed = breed;
await manager.save(dog);
}
const dogs = await manager.query(Dog).all();
console.log("dogs =", dogs);
await db.disconnect();
You should specify the version on the import statement.
import {
Column,
connect,
DataType,
Model,
Primary,
} from "https://deno.land/x/[email protected]/mod.ts";
Hello, I'd like to mention that I'm having this issue as well, I'm declaring versions in my imports (Cotton 0.7.5) and using Deno v1.6.3. It crashes and says I need to specify types for columns but works fine when I specify :)
Hello everyone,
I had the same problem and I fixed it by turning on the decorator metadata settings on Deno. So create a tsconfig.json
file like this
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
And then run your program like this
deno -c tsconfig.json program.ts
Hope this helps. If anyone knows of a better way to achieve the same please let me know.