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

Postgres wrong data type/s #2974

Closed
aetasoul opened this issue Jan 7, 2022 · 1 comment
Closed

Postgres wrong data type/s #2974

aetasoul opened this issue Jan 7, 2022 · 1 comment

Comments

@aetasoul
Copy link

aetasoul commented Jan 7, 2022

Hi,
I'm using Postgresql as database, the tables were manually generated.

  • PG version: 14.1-1.pgdg110+1
  • NHibernate version: 5.3.10
  • FluentNHibernate version: 3.1.0
  • Npgsql version: 6.0.2
------- TABLE AUTHOR -------
CREATE TABLE author (
	id bigint NOT NULL,
	name character varying(50)
);
ALTER TABLE author OWNER TO myuser;

CREATE SEQUENCE author_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
ALTER TABLE author_id_seq OWNER TO myuser;
ALTER SEQUENCE author_id_seq OWNED BY author.id;
ALTER TABLE ONLY author ALTER COLUMN id SET DEFAULT nextval('author_id_seq'::regclass);
ALTER TABLE ONLY author ADD CONSTRAINT author_pkey PRIMARY KEY (id);

------- TABLE BOOK -------
CREATE TABLE book (
	id bigint NOT NULL,
	author_id bigint NOT NULL,
	title character varying(100)
);
ALTER TABLE book OWNER TO myuser;

CREATE SEQUENCE book_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
ALTER TABLE book_id_seq OWNER TO myuser;
ALTER SEQUENCE book_id_seq OWNED BY book.id;
ALTER TABLE ONLY book ALTER COLUMN id SET DEFAULT nextval('book_id_seq'::regclass);
ALTER TABLE ONLY book ADD CONSTRAINT fko5rb7l9pwc20t4465iphn69wt FOREIGN KEY (author_id) REFERENCES author(id);
ALTER TABLE ONLY book ADD CONSTRAINT book_pkey PRIMARY KEY (id);

Classes

public class AuthorClass
{
	public virtual long Id { get; protected set; }
	public IList<BookClass> Books { get; set; }
	public virtual string Name { get; set; }
}

public class BookClass
{
	public virtual long Id { get; protected set; }
	public AuthorClass Author { get; set; }
	public virtual string Title { get; set; }
}

Mappings

public class AuthorMap : ClassMap<AuthorClass>
{
	public AuthorMap()
	{
		Table("author");

		Id(x => x.Id).Column("id").CustomSqlType("bigint").GeneratedBy.SequenceIdentity("author_id_seq");
		Map(x => x.Name).Column("name").CustomSqlType("character varying(50)");
	}
}

public class BookMap : ClassMap<BookClass>
{
	public BookMap()
	{
		Table("book");

		Id(x => x.Id).Column("id").CustomSqlType("bigint").GeneratedBy.SequenceIdentity("book_id_seq");
		References(x => x.Author).Column("author_id").Cascade.All().Not.Nullable();
		Map(x => x.Title).Column("title").CustomSqlType("character varying(100)");
	}
}

Code

ISessionFactory _sessionFactory = Fluently.Configure()
.Database(PostgreSQLConfiguration.Standard.ConnectionString("Server=localhost;Port=5432;Database=MyDatabase;User Id=myuser;Password=mypassword;"))
.Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
.ExposeConfiguration(config =>
{
  SchemaValidator schemaValidator = new SchemaValidator(config);
  schemaValidator.Validate();
})
.BuildSessionFactory();

The schema validation generate an Exception, checking the ValidationErrors array:

- ValidationErrors	Count = 1	System.Collections.ObjectModel.ReadOnlyCollection<string>
    [0] "Wrong column type in MyDatabase.public.book for column author_id. Found: bigint, Expected int8" string

Changing the Mappings without .CustomSqlType:

public class AuthorMap : ClassMap<AuthorClass>
{
	public AuthorMap()
	{
		Table("author");

		Id(x => x.Id).Column("id").GeneratedBy.SequenceIdentity("author_id_seq");
		Map(x => x.Name).Column("name");
	}
}
	
public class BookMap : ClassMap<BookClass>
{
	public BookMap()
	{
		Table("book");

		Id(x => x.Id).Column("id").GeneratedBy.SequenceIdentity("book_id_seq");
		References(x => x.Author).Column("author_id").Cascade.All().Not.Nullable();
		Map(x => x.Title).Column("title");
	}
}

The schema validation generate an Exception for each data type, checking the ValidationErrors array:

-	ValidationErrors	Count = 5	System.Collections.ObjectModel.ReadOnlyCollection<string>
	[0]	"Wrong column type in MyDatabase.public.author for column id. Found: bigint, Expected int8"	string
	[1]	"Wrong column type in MyDatabase.public.author for column name. Found: character varying, Expected varchar(255)"	string
	[2]	"Wrong column type in MyDatabase.public.book for column id. Found: bigint, Expected int8"	string
	[3]	"Wrong column type in MyDatabase.public.book for column author_id. Found: bigint, Expected int8"	string
	[4]	"Wrong column type in MyDatabase.public.book for column title. Found: character varying, Expected varchar(255)"	string

Changing the version of the Npgsql to 4.1.10 all works correctly, would you fix this?

@fredericDelaporte
Copy link
Member

Duplicate of #2876 or #1895, according to viewpoint.

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

2 participants